我需要在我的应用程序运行时和启动时处理通过AlarmManager发布的意图。我已经编写了一个JobIntentService的子类来处理意图,但是它并没有像预期的那样工作:没有调用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被称为。文件中说,这种方法:
进程在作为预O服务运行时启动命令,并将它们排队等待稍后在onHandleWork中分派(意图)。
我不知道“稍后”在这里意味着什么,但是onHandleWork实际上并没有被调用,尽管onStartCommand是出于预期的目的被调用的。
我读过类似问题的答案,并确保我不会凌驾于其他方法之上。我还为服务创建了一个清单条目,我认为它是正确的--否则不会调用onStartCommand。
下面是我如何创建与PendingIntent一起使用的AlarmManager:
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);对我错过了什么有什么想法吗?
发布于 2019-09-04 06:30:11
您可以尝试this推荐。此外,您可能希望检查您的AndroidManifest.xml到android.permission.BIND_JOB_SERVICE权限。
https://stackoverflow.com/questions/53872938
复制相似问题