繁体   English   中英

位置服务没有从奥利奥调用

[英]Location service not getting called from Oreo

我用startService()命令启动了LocationService ,但是因为我知道当app在后台时Oreo不会接受这个服务,所以我切换到了JobSchedular

我在lollipop测试了应用程序并且JobSchedular运行得很好,但在Oreo中,它不运行LocationService

我把break point放在了LocationService的onCreate()方法中,它就不会去那里。

这就是我在做的事情。

主要活动

它执行以下代码,但不对LocationUpdateService.class作出反应

    public void initLocationJob(){

        JobInfo jobInfo;
        JobScheduler jobScheduler;

        ComponentName componentName= new ComponentName(this, LocationUpdateService.class);
        JobInfo.Builder builder= new JobInfo.Builder(11, componentName);

        builder.setPeriodic(5000);
        builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
        builder.setPersisted(true);

        jobInfo= builder.build();
        jobScheduler= (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);


        jobScheduler.schedule(jobInfo);
}

LocationUpdateService

public class LocationUpdateService extends JobService implements
        LocationListener,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        IServiceResponse {



    @Override
    public void onCreate() {
        super.onCreate();

        if (isGooglePlayServicesAvailable()) {

            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();

            mGoogleApiClient.connect();
        }
    }


    @Override
    public boolean onStartJob(JobParameters params) {

        Log.i(TAG, "onStartCommand: ");
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(BACKGROUND_INTERVAL);
        mLocationRequest.setFastestInterval(BACKGROUND_INTERVAL);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

        this.params= params;

        return true;
    }


    @Override
    public boolean onStopJob(JobParameters params) {
        Log.d("JobStopped", "JobStopped");
        return true;
    }

 @Override
    public void onLocationChanged(Location location) {
     //Get current Lat/lng and send it to server
  }

此问题与以下代码:

JobInfo.Builder builder= new JobInfo.Builder(11, componentName);
builder.setPeriodic(5000);

从Android N开始,JobScheduler的最短周期为15分钟。 5秒的频率太频繁且不合适。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM