Android 使用 JobIntentService 在后台更新奥利奥位置
Android Oreo location updates in background with JobIntentService
我曾经使用 AlarmManager + WakefulBroadcastReceiver + Service 来执行一些后台代码并获取位置更新。
自 Oreo 以来,这已被弃用,所以现在我使用 AlarmManager + BroadcastReceiver + JobIntentService.
这是清单中 JobIntentService 的代码:
<service android:name="MyJobIntentService"
android:permission="android.permission.BIND_JOB_SERVICE"/>
以及我需要位置更新的 class MyJobIntentService:
public class MyJobIntentService extends JobIntentService implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
com.google.android.gms.location.LocationListener {
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
@Override
public void onCreate() {
super.onCreate();
}
//Convenience method for enqueuing work in to this service.
static void enqueueWork(Context context, Intent work) {
enqueueWork(context, clsJobIntentServiceDePosicionamientoPlayServices.class, 123456, work);
}
@Override
protected void onHandleWork(Intent intent) {
// We have received work to do. The system or framework is already holding a wake lock for us at this point, so we can just go.
StartLocating();
}
@Override
public void onDestroy() {
super.onDestroy();
}
void StartLocating(){
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
And the rest of the location functions like onLocationChanged...
该应用程序可以运行(至少在模拟器上是这样),但我担心我做错了什么,因为在 onCreate() 之后,它被称为 onHandleWork(),紧接着 onDestroy( ).
几秒钟后,我开始在 onLocationChanged 中获取位置更新,但我担心出了点问题,因为之前调用过 onDestroy()。这是正确的做法吗?
这是我从 BroadcastReceiver 创建 JobIntentService 的方式:
Intent i = new Intent(contexto, MyJobIntentService.class);
MyJobIntentService.enqueueWork(MyContext, i);
JobIntentService 不是您想要的,它取代了 IntentService。这意味着当 onHandleIntent 方法结束时,服务被杀死。在您的情况下,您需要使用前台服务或使用其他服务。
我曾经使用 AlarmManager + WakefulBroadcastReceiver + Service 来执行一些后台代码并获取位置更新。
自 Oreo 以来,这已被弃用,所以现在我使用 AlarmManager + BroadcastReceiver + JobIntentService.
这是清单中 JobIntentService 的代码:
<service android:name="MyJobIntentService"
android:permission="android.permission.BIND_JOB_SERVICE"/>
以及我需要位置更新的 class MyJobIntentService:
public class MyJobIntentService extends JobIntentService implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
com.google.android.gms.location.LocationListener {
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
@Override
public void onCreate() {
super.onCreate();
}
//Convenience method for enqueuing work in to this service.
static void enqueueWork(Context context, Intent work) {
enqueueWork(context, clsJobIntentServiceDePosicionamientoPlayServices.class, 123456, work);
}
@Override
protected void onHandleWork(Intent intent) {
// We have received work to do. The system or framework is already holding a wake lock for us at this point, so we can just go.
StartLocating();
}
@Override
public void onDestroy() {
super.onDestroy();
}
void StartLocating(){
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
And the rest of the location functions like onLocationChanged...
该应用程序可以运行(至少在模拟器上是这样),但我担心我做错了什么,因为在 onCreate() 之后,它被称为 onHandleWork(),紧接着 onDestroy( ).
几秒钟后,我开始在 onLocationChanged 中获取位置更新,但我担心出了点问题,因为之前调用过 onDestroy()。这是正确的做法吗?
这是我从 BroadcastReceiver 创建 JobIntentService 的方式:
Intent i = new Intent(contexto, MyJobIntentService.class);
MyJobIntentService.enqueueWork(MyContext, i);
JobIntentService 不是您想要的,它取代了 IntentService。这意味着当 onHandleIntent 方法结束时,服务被杀死。在您的情况下,您需要使用前台服务或使用其他服务。