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

时间:2018-01-06 18:01:16

标签: android android-widget android-service android-appwidget android-8.1-oreo

我正在使用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中,如果将应用程序从最近的位置移除,它将不再启动该服务。 (不再有效)。 Aren&#t; t 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上进行了测试。

3 个答案:

答案 0 :(得分:12)

您无法再在8.0后台启动服务,但可以使用JobScheduler来获得类似的结果。还有一个JobIntentService帮助程序类,允许您从服务切换到JobScheduler而无需太多重新编译。并且您无法使用PendingIntent指向服务,但您可以使用指向ActivityBroadcastReceiver的服务。

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

  1. 将您的IntentService课程更改为JobIntentService
  2. 将服务onHandleIntent方法重命名为onHandleWork(相同参数)
  3. 在清单中为您的服务添加BIND_JOB_SERVICE权限:
  4.     <service android:name=".widget.MyWidgetService"
               android:permission="android.permission.BIND_JOB_SERVICE">
        </service>
    
    1. 要启动此服务,您必须不再使用context.startService。而是使用enqueueWork静态方法(其中JOB_ID只是一个唯一的整数常量,对于为同一个类排队的所有工作必须是相同的值):
    2.     enqueueWork(context, MyWidgetService.class, JOB_ID, intent);
      
      1. 对于点击,请将指向service的pendingIntent替换为指向BroadcastReceiver的pendingIntent。由于您的AppWidgetProvider子类本身就是BroadcastReceiver,您也可以使用它:
      2.     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+上运行得很好):
        2.     @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在旧设备上使用)。
          2. 就是这样。 此小部件现在可以在新旧设备上正常运行。唯一真正的区别在于,如果您的8.0设备处于打盹模式,它可能不会经常更新小部件,但这应该不是问题,因为如果它是打瞌睡,这意味着用户现在无法看到您的小部件反正。

答案 1 :(得分:7)

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

  • 使用getForegroundService(),或者

  • 如果您不想提出getBroadcast(),请使用明确的Intent BroadcastReceiverJobIntentService Notification开始getService() (作为前台服务要求)

根据您的症状,您似乎已经超过我认为的错误:应该有一种方法可以从getForegroundService()切换到{{1}}而无需重新启动。 :-)如果我可以重现这个问题,我会尝试进行一些实验并提出问题。

答案 2 :(得分:2)

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

How to use "goAsync" for broadcastReceiver?