0

我试图在应用程序关闭时以特定的时间间隔抓取位置,并尝试使用GoogleApiClient的获取上次已知位置并触发使用一个JobService。我已经在JobScheduler类上实现了ConnectionCallbacks。但问题是onConnected()回调从不被调用。我认为服务在返回任何回调之前被销毁。那么什么是实施这种逻辑的有效方式。在使用Job Scheduler API的Job Service中使用GoogleApiClient - 回调丢失

我的工作的服务类

public class TestJobService extends JobService 
{ 
protected GoogleApiClient mGoogleApiClient; 
protected Location mLastLocation; 
protected JobParameters mJobParameters; 
public TestJobService() { 
    super(); 
} 
private class GetLocation extends AsyncTask<Integer, Void, Integer> implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener { 
    protected Integer doInBackground(Integer... jobID) { 
     if (mGoogleApiClient == null) { 
      mGoogleApiClient = new GoogleApiClient.Builder(getBaseContext()) 
        .addConnectionCallbacks(this) 
        .addOnConnectionFailedListener(this) 
        .addApi(LocationServices.API) 
        .build(); 
     } 

     return jobID[0]; 
    } 



    protected void onPostExecute(Integer jobID) { 
     Log.i("JobSchedulerTest","Job Finished!"); 
     jobFinished(mJobParameters, true); 
    } 

    @Override 
    public void onConnected(@Nullable Bundle bundle) { 
     mLastLocation=LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
     Log.i("JobSchedulerTest", "on start job: " + mJobParameters.getJobId() + "," + 
       DateFormat.getTimeInstance().format(new Date())+ 
       ",Location("+mLastLocation.getLatitude()+","+mLastLocation.getLongitude()+")"); 

    } 

    @Override 
    public void onConnectionSuspended(int i) { 

    } 

    @Override 
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

    } 
} 


@Override 
public boolean onStartJob(JobParameters jobParameters) { 
    Log.i("JobSchedulerTest","Job Running!"); 
    mJobParameters=jobParameters; 
    Integer i=new Integer(mJobParameters.getJobId()); 
    new GetLocation().execute(i); 
    return true; 

} 

@Override 
public boolean onStopJob(JobParameters jobParameters) { 
    Log.i("JobSchedulerTest","Job Stopped!"); 
    return true; 
} 

} 

我呼吁在活动中给予的工作,这些参数

private void scheduleJob() { 
     ComponentName mServiceComponent = new ComponentName(this, TestJobService.class); 
     JobInfo.Builder builder = new JobInfo.Builder(5,mServiceComponent); 
     //builder.setMinimumLatency(5 * 1000); // wait at least 
     //builder.setOverrideDeadline(50 * 1000); // maximum delay 
     builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);// require unmetered network 
     builder.setBackoffCriteria(5000,JobInfo.BACKOFF_POLICY_LINEAR); 
     builder.setRequiresDeviceIdle(false); // we dont care if device should be idle 
     builder.setRequiresCharging(false);// we don't care if the device is charging or not 
     //builder.setPersisted(true); 
     builder.setPeriodic(50*1000); 

     JobScheduler jobScheduler = (JobScheduler)getApplication().getSystemService(Context.JOB_SCHEDULER_SERVICE); 
     jobScheduler.schedule(builder.build()); 
    } 

记录有关的工作:

04-16 00:48:35.266 10455-10455/com.google.android.gms.location.sample.basiclocationsample I/JobSchedulerTest: Job Running! 
04-16 00:48:35.293 10455-10455/com.google.android.gms.location.sample.basiclocationsample I/JobSchedulerTest: Job Finished! 
04-16 00:48:37.484 2028-2951/? V/AlarmManager: sending alarm {c169dc type 3 *alarm*:android.content.jobscheduler.JOB_DELAY_EXPIRED} 
04-16 00:48:37.550 2028-2028/? V/AlarmManager: done {c169dc, *alarm*:android.content.jobscheduler.JOB_DELAY_EXPIRED} [67ms] 
04-16 00:48:37.555 10455-10455/com.google.android.gms.location.sample.basiclocationsample I/JobSchedulerTest: Job Running! 
04-16 00:48:37.563 10455-10455/com.google.android.gms.location.sample.basiclocationsample I/JobSchedulerTest: Job Finished! 
04-16 00:48:39.927 2028-2951/? V/AlarmManager: sending alarm {c169dc type 3 *alarm*:android.content.jobscheduler.JOB_DELAY_EXPIRED} 
04-16 00:48:39.947 2028-2028/? V/AlarmManager: done {c169dc, *alarm*:android.content.jobscheduler.JOB_DELAY_EXPIRED} [23ms] 
04-16 00:48:39.952 10455-10455/com.google.android.gms.location.sample.basiclocationsample I/JobSchedulerTest: Job Running! 
04-16 00:48:39.957 10455-10455/com.google.android.gms.location.sample.basiclocationsample I/JobSchedulerTest: Job Finished! 
04-16 00:48:50.078 2028-2951/? V/AlarmManager: sending alarm {c169dc type 3 *alarm*:android.content.jobscheduler.JOB_DELAY_EXPIRED} 
04-16 00:48:50.114 2028-2028/? V/AlarmManager: done {c169dc, *alarm*:android.content.jobscheduler.JOB_DELAY_EXPIRED} [154ms] 
04-16 00:48:50.117 10455-10455/com.google.android.gms.location.sample.basiclocationsample I/JobSchedulerTest: Job Running! 
04-16 00:48:50.120 10455-10455/com.google.android.gms.location.sample.basiclocationsample I/JobSchedulerTest: Job Finished! 

我onConnected实现是永远被解雇。

+0

你'AsyncTask'尽快解散是因为'doInBackground()'返回,和你的工作被终止微秒后通过'jobFinished()'。那时,你的整个过程可能会被拆除。恕我直言,'AsyncTask'在这里是错误的解决方案(只是使用'Thread'),你不能完成工作,直到你得到你的位置(或者在你选择一段时间后,以防万一你永远不会得到一个位置)。 – CommonsWare

+0

我不知道他们是谁。 – CommonsWare

+0

@CommonsWare你可以请用线程显示实现,我很困惑。 – theSereneRebel

回答

1

您必须在清单和代码中要求位置权限才能使其工作。

清单

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 

MainActivity类别:

的OnCreate():

private static final int MY_PERMISSIONS_REQUEST_FINE_LOCATION = 111; 

    if (ContextCompat.checkSelfPermission(MainActivity.this, // Check for permission 
      android.Manifest.permission.ACCESS_FINE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED) { 
     ActivityCompat.requestPermissions(
       this, // Activity 
       new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 
       MY_PERMISSIONS_REQUEST_FINE_LOCATION); 
    } 
相关问题