【发布时间】:2019-01-08 17:04:05
【问题描述】:
我最近将我的所有服务替换为前台服务和 JobIntentService,因为在 oreo 及更高版本中存在一些后台执行限制 (https://developer.android.com/about/versions/oreo/background)。根据文档,JobIntentService 的作用类似于 Android 7 及更低版本的 Intent Service,并且类似于 Android 8 及更高版本的 JobScheduler。我注意到 Google 提供的新 JobIntentService 存在问题。
Android 8 及更高版本:
在 android 8 及更高版本中不断发生崩溃。这里提出了一张票,提到了同样的问题https://issuetracker.google.com/issues/63622293,我添加了一些极客建议的临时修复。
Android 7 及以下: JobIntentService 在工作完成后不会停止,就像 Intent Service 一样。
我在服务中实现了 JobIntentService,只要用户执行某些操作就会触发。
代码
public class SampleJobIntentService extends FixedJobIntentService {
public static void postData(Context context, String data) {
Intent intent = new Intent(context, SampleJobIntentService.class);
intent.setAction(INITIAL_ACTION);
intent.putExtra(SAMPLE_ID, data);
SampleJobIntentService.enqueueWork(context,intent);
}
public static void enqueueWork(Context context, Intent work) {
SampleJobIntentService.enqueueWork(context, SampleJobIntentService.class, JOB_ID, work);
@Override
protected void onHandleWork(@NonNull Intent intent) {
if (intent != null) {
SampleRequest sampleRequest = requests.get(intent.getAction());
if (sampleRequest != null) {
try {
// perform some networking operations
} catch (Exception ex) {
Log.d("Error for intent ");
}
Log.i("send action ");
} else
Log.e("action not found for ");
}
}
}
为了避免 JobIntentService 崩溃,我从https://issuetracker.google.com/issues/63622293 中引用了一些参考资料
public abstract class FixedJobIntentService extends JobIntentService {
@Override
GenericWorkItem dequeueWork() {
try {
return new FixedGenericWorkItem(super.dequeueWork());
} catch (SecurityException ignored) {
doStopCurrentWork();
}
return null;
}
private class FixedGenericWorkItem implements GenericWorkItem {
final GenericWorkItem mGenericWorkItem;
FixedGenericWorkItem(GenericWorkItem genericWorkItem) {
mGenericWorkItem = genericWorkItem;
}
@Override
public Intent getIntent() {
if (mGenericWorkItem != null) {
return mGenericWorkItem.getIntent();
}
return null;
}
@Override
public void complete() {
try {
if (mGenericWorkItem != null) {
mGenericWorkItem.complete();
}
} catch (IllegalArgumentException ignored) {
doStopCurrentWork();
}
}
}
}
【问题讨论】:
-
你为什么要触发一个服务来触发一个 JobIntentService。 JobIntentService 可以直接在用户操作时触发?
-
@Ankur 即使我直接触发 JobIntentService,一旦工作完成,它也不会被杀死。我已经提到了在我的项目中使用它的一种情况。我的项目中使用了几个 JobIntentServices
-
你能提供你正在使用的JobIntentService的示例代码吗?
-
我已经更新了我的问题
-
@Kalai.G,好的......我明白了......我正在逐步解释它作为答案......因为我看不到这里剩下多少空间......我不想让完整的线程充满喜欢闲聊。
标签: android intentservice jobintentservice