首页 > 解决方案 > JobService 每分钟运行一次,没有可能杀死

问题描述

我必须为一家小型物流公司开发一个应用程序,该公司在交付时需要他们的专业移动轨道驱动程序。我需要获取手机的位置并将它们发送到数据库。该手机仅用于工作,不得用于个人用途或商业目的。

我试图实现一个启动线程的 JobService,但我读到它只能每 15 分钟运行一次。有没有办法实现这个功能?

这是我的 MainActivity ,它只包含一个启动服务的按钮:

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "jobservice";
    private static final int REQUEST_PERMISSION_LOCATION = 255;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void scheduleJob(View v){

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_PERMISSION_LOCATION);
            }
        } else {
            ComponentName componentName = new ComponentName(this, LocationService.class);
            JobInfo info = new JobInfo.Builder(123, componentName)
                    .setPersisted(true)
                    .setPeriodic(15 * 60 * 1000)
                    .setRequiredNetworkType(NETWORK_TYPE_ANY)
                    .build();
            JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);

            int resultCode = scheduler.schedule(info);
            if(resultCode == JobScheduler.RESULT_SUCCESS){
                Log.d(TAG, "Job Schedule");
            }
        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_PERMISSION_LOCATION) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            }
        }
    }
}

还有我的 LocationService 文件:

public class LocationService extends JobService implements LocationListener {

    private static final String TAG = "jobservice";
    LocationManager locationManager;
    TextView locationText;
    private static Double latitude = 0.0;
    private static Double longitude = 0.0;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "Service created");
    }

    @Override
    public boolean onStartJob(JobParameters params) {
        Log.d(TAG, "onStartJob: ");

        doBackgroundJob(params);
        return true;
    }

    private void doBackgroundJob(JobParameters params){
        new Thread(new Runnable() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void run() {
                getLocation();
                jobFinished(params, true);
            }
        }).start();
    }


    @RequiresApi(api = Build.VERSION_CODES.M)
    void getLocation() {
        try {
            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 5, this);
        }
        catch(SecurityException e) {
            e.printStackTrace();
        }
    }


    @Override
    public boolean onStopJob(JobParameters params) {
        Log.d(TAG, "Job cancelled before completion");
        return true;
    }

    @Override
    public void onLocationChanged(@NonNull Location location) {
        if((location.getLatitude() == latitude) && (location.getLongitude() == longitude)){

        }else{
            longitude = location.getLongitude();
            latitude = location.getLatitude();
            Log.d(TAG, "New DATA");
            Log.d(TAG, "Current Location: " + location.getLatitude() + ", " + location.getLongitude());
        }

    }

    @Override
    public void onProviderEnabled(@NonNull String provider) {

    }

    @Override
    public void onProviderDisabled(@NonNull String provider) {
        Toast.makeText(this, "Please Enable GPS and Internet", Toast.LENGTH_SHORT).show();
    }
}
```

Thank you in advance

标签: androidlocation

解决方案


推荐阅读