使用PendingIntent在app小部件中的Oreo上启动服务

问题描述 投票:16回答:3

我正在使用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中,如果应用程序从最近移除,它将不再启动该服务。 (不再有效)。 Oreo的背景执行限制中不包括PendingIntents吗?

出于测试目的,我用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上进行了测试。

android android-widget android-service android-appwidget android-8.1-oreo
3个回答
7
投票

如您所知,app小部件不计入PendingIntent背景白名单。我不知道为什么 - 它似乎与PendingIntent开始的Notification相提并论。也许这是一个问题,Notification是一个系统的东西,而app小部件是一个主屏事件。无论如何,你可以:

  • 像你一样使用getForegroundService(),或者
  • 尝试getBroadcast()与明确的Intent,并与BroadcastReceiver开始JobIntentService,如果你不想提出Notification(作为前台服务要求)

根据你的症状,你似乎已经绊倒了我认为是一个错误:应该有一种方法从getService()切换到getForegroundService()而不必重新启动。 :-)我会尝试运行一些实验,如果我能重现问题就会提出问题。


14
投票

您无法再在8.0中的后台启动服务,但您可以使用JobScheduler来获得类似的结果。还有一个JobIntentService助手类,允许您从服务切换到JobScheduler没有太多的refatoring。你不能使用指向服务的PendingIntent,但你可以使用一个指向ActivityBroadcastReceiver

如果你有一个8.0之前的工作小部件,现在你需要让它在Android 8.0上工作,只需执行这个简单的步骤:

  1. 将你的IntentService课程改为JobIntentService
  2. 将服务onHandleIntent方法重命名为onHandleWork(相同参数)
  3. 在清单中为您的服务添加BIND_JOB_SERVICE权限:
    <service android:name=".widget.MyWidgetService"
           android:permission="android.permission.BIND_JOB_SERVICE">
    </service>
  1. 要启动此服务,您必须不再使用context.startService。而是使用enqueueWork静态方法(其中JOB_ID只是一个唯一的整数常量,对于为同一个类排队的所有工作必须是相同的值):
    enqueueWork(context, MyWidgetService.class, JOB_ID, intent);
  1. 对于点击,请将指向service的pendingIntent替换为指向BroadcastReceiver的pendingIntent。由于AppWidgetProvider的子类本身就是BroadcastReceiver,因此您也可以使用它:
    Intent myIntent = new Intent(context, MyAppWidgetProvider.class);
    myIntent .setAction("SOME_UNIQUE_ACTION");
    pendingIntent = PendingIntent.getBroadcast(context, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  1. 在onReceive中使用enqueueWork启动服务(如果你的PendingIntent启动了activity,只需留下它 - 它在android 8.0+上工作得很好):
    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        if (intent.getAction().equals("SOME_UNIQUE_ACTION")) {
            MyWidgetService.enqueueWork(.....);
        }
    }
  1. 要确保widget可以在旧设备上运行,请确保您的清单中有WAKE_LOCK权限(旧设备上由JobIntentService使用)。

而已。此小部件现在可以在新旧设备上正常运行。唯一真正的区别在于,如果您的8.0设备处于打盹模式,它可能不会经常更新小部件,但这应该不是问题,因为如果它是打瞌睡,这意味着用户现在无法看到您的小部件无论如何。


2
投票

可以选择goasyc吗?您可以更改PendingIntent以触发BroadcastReceiver,然后在OnRecieve方法中,您可以调用goasync()然后您应该能够使用它生成的PendingResult来创建异步调用。仍然不认为你可以开始服务。

How to use "goAsync" for broadcastReceiver?

© www.soinside.com 2019 - 2024. All rights reserved.