服务未使用PendingIntent在应用程序小部件中的Oreo上启动 [英] Service not starting on Oreo in app widget using PendingIntent

查看:137
本文介绍了服务未使用PendingIntent在应用程序小部件中的Oreo上启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Android应用小部件。我正在创建一个 PendingIntent 对象,并在方法 RemoteViews#setOnClickPendingIntent()中使用它。这是未决的意图:

I'm using Android app widgets. I'm creating a PendingIntent object and use it in the method RemoteViews#setOnClickPendingIntent(). This is the pending intent:

// The below code is called in the onUpdate(Context, AppWidgetManager, int[]) method of the AppWidgetProvider class.

// Explicit intent
Intent intent = new Intent(context, MyService.class);
intent.setAction(MY_ACTION);
intent.putExtra(EXTRA_KEY, VALUE);

// Create the pending intent
PendingIntent pendingIntent = PendingIntent.getService(context, appWidgetId, intent, 0);

// 'views' is a RemoteViews object provided by onUpdate in the AppWidgetProvider class.
views.setOnClickPendingIntent(R.id.root_layout, pendingIntent);

以上代码在Android Oreo之前按预期工作。但是,在Android Oreo中,如果将应用程序从最新消息中清除,它将不再启动该服务。 (不再有效)。 PendingIntent 不在奥利奥的后台执行限制中吗?

The above code works as expected before Android Oreo. However, in Android Oreo it no longer starts the service if the app is swiped away from recents. (no longer active). Aren't PendingIntents excluded in Oreo's background execution limits?

出于测试目的,我替换了 getForegroundService 的> getService ,但 Service 仍未启动。两种方法都在日志中显示以下消息:

For testing purposes I replaced getService with getForegroundService but the Service is still not started. Both methods show the below message in the log:

W/ActivityManager: Background start not allowed: service Intent { act=com.example.myapp.MY_ACTION flg=0x10000000 cmp=com.example.myapp/com.example.myapp.MyService bnds=[607,716][833,942] (has extras) } to com.example.myapp/com.example.myapp.MyService from pid=-1 uid=10105 pkg=com.example.myapp

为什么<$ c即使使用 getForegroundService ,$ c>服务也无法启动?我在运行Android 8.1.0的Nexus 6P上对此进行了测试。

Why is the Service not started, even when using getForegroundService? I tested this on a Nexus 6P running Android 8.1.0.

推荐答案

您注意到,应用小部件不计入 PendingIntent 背景白名单。我不知道为什么—它似乎与 Notification 开始的 PendingIntent 差不多。 通知是系统问题,而应用程序小部件是主屏幕问题可能是一个问题。无论如何,您都可以:

As you note, an app widget does not count for the PendingIntent background whitelist. I do not know why — it would seem to be about on par with a PendingIntent started by a Notification. Perhaps it's an issue that the Notification is a system thing, whereas the app widget is a home screen thing. Regardless, you could:


  • 使用 getForegroundService()

使用明确的意图尝试 getBroadcast() ,如果您不想提高<$,则使用 BroadcastReceiver 开始 JobIntentService c $ c>通知(作为前台服务需要)

Try getBroadcast() with an explicit Intent, and with the BroadcastReceiver starting a JobIntentService, if you do not want to raise a Notification (as a foreground service requires)

根据您的症状,您似乎已经跳过了我认为是个错误的问题:应该有一种方法可以从 getService()切换到 getForegroundService() 无需重启。 :-)我将尝试进行一些实验,如果可以重现该问题,则会提出问题。

Based on your symptoms, you appear to have tripped over what I would consider to be a bug: there should be a way to switch from getService() to getForegroundService() without having to reboot. :-) I'll try to run some experiments and will file an issue if I can reproduce the problem.

这篇关于服务未使用PendingIntent在应用程序小部件中的Oreo上启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆