ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

android – 服务不是使用PendingIntent在应用程序小部件中的Oreo上启动的

2019-10-05 04:25:49  阅读:275  来源: 互联网

标签:android-appwidget android android-service android-widget 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中,如果将应用程序从最近的位置移除,它将不再启动该服务. (不再有效). Oreo的后台执行限制中是否排除了PendingIntents?

出于测试目的,我用getForegroundService替换了getService,但仍未启动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

为什么服务没有启动,即使使用getForegroundService?我在运行Android 8.1.0的Nexus 6P上进行了测试.

解决方法:

如您所知,应用小部件不计入PendingIntent背景白名单.我不知道为什么 – 它似乎与通知启动的PendingIntent相提并论.也许这是一个问题,通知是一个系统的事情,而应用程序小部件是一个主屏事件.无论如何,你可以:

>使用getForegroundService(),或者使用
>尝试使用显式Intent的getBroadcast(),并使用BroadcastReceiver启动JobIntentService,如果您不想引发通知(作为前台服务需要)

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

标签:android-appwidget,android,android-service,android-widget,android-8-1-oreo
来源: https://codeday.me/bug/20191005/1854921.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有