问题描述
类似的问题:service not starting on oreo in app widgetusing与我的问题最接近的是Widget freezes after some time,但没有得到答案。
我有一个简单的小部件按钮,按下该按钮即可发送通知。在以下情况下不起作用:
- 在安装后打开应用程序后,我从最近使用的应用程序中清除了
- 随机有时会停止工作(有时,在我再次打开该应用程序后,所有的点击都会一起触发,从而导致一堆通知被一起触发。)
从目前为止我所读到的所有内容看来,PendingIntent已被清除。
除了启动服务而不是活动外,大部分代码都来自official app widget guide。
WidgetProvider :
public class WidgetProvider extends AppWidgetProvider {
@RequiresApi(api = Build.VERSION_CODES.O)
public void onUpdate(Context context,AppWidgetManager appWidgetManager,int[] appWidgetIds) {
final int N = appWidgetIds.length;
for (int appWidgetId : appWidgetIds) {
Intent myIntent = new Intent(context,WidgetService.class);
myIntent .setAction("SOME_UNIQUE_ACTION");
PendingIntent pendingIntent = PendingIntent.getForegroundService(context,myIntent,PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
views.setOnClickPendingIntent(R.id.button,pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId,views);
}
}
}
WidgetService :
public class WidgetService extends JobIntentService {
public WidgetService() {
super();
}
@Override
protected void onHandleWork(@Nullable Intent i) {
Utils.sendNotification(); // this method works and is tested
}
}
我尝试了here的两个答案,Broadcast方法也不起作用,并且看起来也是间接的,所以我向#onCreate
的实现中添加了调用startForeground的实现,该实现也不起作用。
@Override
public void onCreate() {
super.onCreate();
Utils.sendNotification();
try {
startForeground(1,notification);
} catch (Error e) {
Log.i(TAG,e.toString());
}
}
}
我如何获得一个可以每次可靠地启动服务的按钮?希望获得任何帮助。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)