我有一个JobIntentService的主干类
public class BackgroundRequestService extends JobIntentService {
/**
* Unique job ID for this service.
*/
static final int JOB_ID = 1234;
public static void enqueueWork(Context context, Intent work) {
BackgroundRequestService.enqueueWork(context, BackgroundRequestService.class, JOB_ID, work);
}
@Override
protected void onHandleWork(@NonNull Intent intent) {
if (intent.getExtras() != null){
String x = ";";
}
}}
我已将该服务包含在清单中
<service android:name=".BackgroundRequestService"
android:enabled="true"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="false" />并调用适当的入队方法
Intent intent = new Intent();
intent.putExtra("hardware", hardware);
BackgroundRequestService.enqueueWork(context, intent);但是onHandleWork永远不会被调用。我已经查看了所有关于正确设置服务并确保onBind不被覆盖的页面,但这仍然不起作用,我想知道是否另一双眼睛会发现我遗漏了什么。谢谢
发布于 2018-12-13 11:55:17
尝试这样启动服务:
ComponentName comp = new ComponentName(context.getPackageName(), BackgroundRequestService.class.getName());
BackgroundRequestService.enqueueWork(context, (getIntent().setComponent(comp)));发布于 2018-12-13 12:00:28
通过对Logcat进行适当的检查发现了该问题
事实证明,我放在意图中的“硬件”缺少一个要序列化的字段。
这会导致此消息出现在Logcat中
I/UDP: no longer listening for UDP broadcasts cause of error Parcelable encountered IOException writing serializable object (name = Project.Hardware)在修复这个序列化问题之后,我能够调用onHandleWork()方法。
https://stackoverflow.com/questions/53754737
复制相似问题