19

我正在使用 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排除在奥利奥的后台执行限制中吗?

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

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 上对此进行了测试。

4

3 回答 3

17

在 8.0 中您不能再在后台启动服务,但您可以使用它JobScheduler来实现类似的结果。还有一个JobIntentService帮助类,允许您JobScheduler从服务切换到没有太多重构。而且您不能使用PendingIntent指向服务,但可以使用指向Activityor的一个BroadcastReceiver

如果您在 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. 对于点击,将指向服务的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 设备处于打瞌睡模式,它可能不会经常更新小部件,但这应该不是问题,因为如果它正在打瞌睡,这意味着用户现在看不到您的小部件无论如何。

于 2018-04-10T14:52:53.680 回答
10

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

  • 使用getForegroundService(), 如你所愿, 或

  • 如果您不想引发 a (作为前台服务要求) ,请尝试getBroadcast()使用显式Intent,并以BroadcastReceivera 开头JobIntentServiceNotification

根据您的症状,您似乎遇到了我认为是错误的问题:应该有一种无需重新启动即可切换getService()到的方法。getForegroundService():-) 我会尝试进行一些实验,如果我能重现问题,我会提出问题。

于 2018-01-06T19:48:17.760 回答
2

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

如何将“goAsync”用于广播接收器?

于 2018-02-14T07:03:31.603 回答