Ibinder和JobintentService

时间:2019-06-24 00:11:59

标签: java android

我的问题很简单,但是我为此一直苦苦挣扎!

我有一个绑定服务,该服务调用onBound()方法:

    @Override
public IBinder onBind(Intent intent) {
    return null;
}

Unlikley,无法在JobIntentService中使用该实现,因为它将抛出JobServiceContext: Time-Out with binding service

当然,删除该替代方法不是解决方案,因为我无法删除它...

这是我的JobIntenteservice

JobBoot.java

public class JobBoot extends JobIntentService {

public static final int JOB_ID = 0x01;

public static void enqueueWork(Context context, Intent work) {
    enqueueWork(context, BackgroundService.class, JOB_ID, work);
}

@Override
protected void onHandleWork(@NonNull Intent intent) {
    // your code
}}

我该怎么做才能解决此问题?

谢谢

1 个答案:

答案 0 :(得分:0)

在JobBoot类的静态方法enqueueWork中,它应使用JobBoot而不是BackgroundService。

public static void enqueueWork(Context context, Intent work) { enqueueWork(context, JobBoot.class, JOB_ID, work); }

以及JobBoot的onHandleWork

   protected void onHandleWork(@NotNull Intent intent) {

     // 
     Intent intent = new Intent(this, BackgroundService.class)
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
         startForegroundService(intent)
     } else {
         startService(intent)
     }
}