JobIntentService not calling onHandleWork



我是服务的新手,决定使用 JobIntentService,因为我的应用程序必须支持 API 21+,但是当我在 API 28+ 上启动我的服务时,它无法正常工作。

我发现当我开始使用Contextcompat.startForegroundService()启动服务时,不会调用onHandleWork

我的代码:

我的活动

Intent intent = new Intent(this, MyService.class);
intent.putExtra("fileTitle", popup.name);
ContextCompat.startForegroundService(this,intent);

我的服务

@Override
public void onCreate() {
super.onCreate();
MyLog(this, "SERVICE ONCREATE", "SERVICE");
createNotificationChannel();
initNotification();
startForeground(NOTIFICATION_ID, nBuilder.build());
}

为了结束这个主题,这就是我设法使其工作的方式:

我的活动

Intent intent = new Intent(this, MyService.class);
intent.putExtra("fileTitle", popup.name);
ContextCompat.startForegroundService(this,intent);

我的服务

@Override
public void onCreate() {
super.onCreate();
// Init for Notification Channel in API 26+ && init notification to display
createNotificationChannel();
initNotification();
}

@Override
protected void onHandleIntent(@Nullable Intent intent) {
if(intent!= null && isPostorEqualOS(Build.VERSION_CODES.O)){
startForeground(NOTIFICATION_ID, nBuilder.build());
}
}

最新更新