gpt4 book ai didi

android - 未在 JobIntentService 中调用 onHandleWork()

转载 作者:行者123 更新时间:2023-11-30 05:10:10 25 4
gpt4 key购买 nike

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

我的实现在处理程序是一个 IntentService 时有效,除了在启动时由于后台服务的限制。因此,我尝试改用 JobIntentService。

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 被调用。文档说这个方法:

Processes start commands when running as a pre-O service, enqueueing them to be later dispatched in onHandleWork(Intent).

我不知道这里的“稍后”是什么意思,但实际上并没有调用 onHandleWork,尽管已经以预期的目的调用了 onStartCommand。

我已阅读类似问题的答案并确保我不会覆盖其他方法。我还为服务创建了一个 list 条目,我认为它是正确的 - 否则不会调用 onStartCommand。

这就是我创建与 AlarmManager 一起使用的 PendingIntent 的方式:

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);

关于我遗漏的任何想法?

最佳答案

你可以试试this推荐。此外,您可能需要检查您的 AndroidManifest.xml 是否具有 android.permission.BIND_JOB_SERVICE 权限。

关于android - 未在 JobIntentService 中调用 onHandleWork(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53872938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com