首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >onHandleWork()没有在JobIntentService中调用

onHandleWork()没有在JobIntentService中调用
EN

Stack Overflow用户
提问于 2018-12-20 16:53:19
回答 1查看 1.9K关注 0票数 1

我需要在我的应用程序运行时和启动时处理通过AlarmManager发布的意图。我已经编写了一个JobIntentService的子类来处理意图,但是它并没有像预期的那样工作:没有调用onHandleWork。

当处理程序是IntentService时,我的实现工作正常,但由于后台服务的限制,在启动时除外。因此,我尝试使用JobIntentService代替。

代码语言:javascript
复制
public class MyJobIntentService extends JobIntentService
{
    public int onStartCommand(@Nullable Intent intent, int flags, int startId)
    {
        // This gets called with the intent given to AlarmManager
        return super.onStartCommand(intent, flags, startId);
    }

    public static void enqueueWork(Context context, Intent work)
    {
        // Not called
        enqueueWork(context, NotificationService.class, JOB_ID, work);
    }

    public void onHandleWork(@NonNull Intent intent)
    {
        // Not called
        ...
    }

    // No other methods overridden
}

onStartCommand被称为。文件中说,这种方法:

进程在作为预O服务运行时启动命令,并将它们排队等待稍后在onHandleWork中分派(意图)。

我不知道“稍后”在这里意味着什么,但是onHandleWork实际上并没有被调用,尽管onStartCommand是出于预期的目的被调用的。

我读过类似问题的答案,并确保我不会凌驾于其他方法之上。我还为服务创建了一个清单条目,我认为它是正确的--否则不会调用onStartCommand。

下面是我如何创建与PendingIntent一起使用的AlarmManager:

代码语言:javascript
复制
Intent myIntent = new Intent( myContext, MyJobIntentService.class);
...
PendingIntent pendingIntent = PendingIntent.getService( mContext, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
myAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);

对我错过了什么有什么想法吗?

EN

Stack Overflow用户

发布于 2019-09-04 06:30:11

您可以尝试this推荐。此外,您可能希望检查您的AndroidManifest.xml到android.permission.BIND_JOB_SERVICE权限。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53872938

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档