我正在使用 Android 应用小部件。我正在创建一个PendingIntent
对象并在方法中使用它RemoteViews#setOnClickPendingIntent()
。这是待定意图:
// 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
排除在奥利奥的后台执行限制中吗?
出于测试目的,我替换getService
为getForegroundService
但Service
仍未启动。两种方法都在日志中显示以下消息:
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
为什么Service
即使在使用时也没有启动getForegroundService
?我在运行 Android 8.1.0 的 Nexus 6P 上对此进行了测试。