Я пытаюсь создать простой виджет для Oreo и выше, который перезагружает свои данные периодически и по щелчкам.Ниже приведен набросок кода, который у меня есть:
public class WodWidget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
updateWodWidget(context);
}
static void updateWodWidget(Context context) {
context.startForegroundService(new Intent(context, UpdateService.class));
}
public static class UpdateService extends IntentService {
private static final String TAG = "WodWidgetUpdateService";
private final Handler mHandler = new Handler();
public UpdateService() { super(TAG); }
@Override
public IBinder onBind(Intent intent) { return null; }
@Override
public void onCreate() {
super.onCreate();
String channelId = "foobar";
String channelName = "Widget update service";
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_NONE);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
Notification notification = new Notification.Builder(getApplicationContext())
.setChannelId(channelId)
.setContentTitle("notification title")
.setContentText("notification content")
.build();
startForeground(1, notification);
}
@Override
protected void onHandleIntent(Intent intent) {
if (Clicks.ACTION_CLICK.equals(intent.getAction())) {
Clicks.handleClickAction(this, mHandler);
} else {
getWodData(this);
}
}
protected void getWodData(final Context context) {
WodData.getInstance().invalidate();
WodWidget.updateViews(context);
GetHTMLTask task = new GetHTMLTask(new GetHTMLTask.OnTaskCompleted() {
@Override
public void onTaskCompleted() {
WodWidget.updateViews(context);
}
});
task.execute(WodData.PARSE_URL);
}
}
static void singleClickHandler(Context context) {
updateWodWidget(context);
}
static void doubleClickHandler(Context context) { [...] }
private static void updateViews(Context context) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.wod_widget);
WodData wd = WodData.getInstance();
view s.setTextViewText(R.id.appwidget_text, "foobar");
Intent intent = new Intent(Clicks.ACTION_CLICK, null, context, UpdateService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.layoutWidget, pendingIntent);
ComponentName thisWidget = new ComponentName(context, WodWidget.class);
AppWidgetManager.getInstance(context).updateAppWidget(thisWidget, views);
}
}
Теперь это работает и работает нормально, и некоторое время, также щелкая виджет, работает.Однако через некоторое время (несколько минут или около того) нажатие больше не обновляет виджет, и я вижу следующую запись в logcat:
W/ActivityManager: Background start not allowed: service Intent { act=com.mywidget.action.CLICK flg=0x10000000 cmp=com.mywidget/.WodWidget$UpdateService bnds=[56,367][620,1111] } to com.mywidget/.WodWidget$UpdateService from pid=-1 uid=10083 pkg=com.mywidget
Так что я думаю, что она становится фоновой службой, а не службой переднего плана, но почему и как не допустить этого?