【问题标题】:Location service not getting called from Oreo奥利奥没有调用定位服务
【发布时间】:2018-05-30 08:11:16
【问题描述】:

我是用startService() 命令启动LocationService,但由于我知道当应用程序处于后台时奥利奥不会提供此服务,所以我切换到JobSchedular

我在lollipopJobSchedular 中测试了该应用程序运行良好,但在奥利奥中,它无法运行LocationService

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

这就是我正在做的。

MainActivity

它执行以下代码,但不响应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
  }

【问题讨论】:

    标签: android performance android-location android-8.0-oreo android-jobscheduler


    【解决方案1】:

    此问题与以下代码有关:

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

    从 Android N 开始,JobScheduler 以至少 15 分钟的周期运行。 5秒的频率太频繁了,不合适。

    【讨论】:

    • 谢谢@Sagar,我会试试这个,但它不应该至少运行这个服务1次吗?
    • 您无法控制在此时间间隔内何时执行此作业,只能保证在此时间间隔内最多执行一次
    • 我在Nougat设备上试过这个,10分钟后成功发送数据,但是oreo没有发送任何更新,可能是因为我没有物理oreo设备,我正在测试它模拟器。
    • @Kirmani88 感谢更新。在模拟器和真实设备上的行为不应有很大不同。但值得检查一下。
    • 它调用onCreateonStartJob,但不调用Oreo上的onLocationChanged方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 2020-02-14
    • 2019-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多