我在活动的onResume上启动了一项服务,它在oreo上运行良好,但最近我看到Android P崩溃,该消息显示“无法恢复活动。...不允许启动服务意图...应用在背景中。”有没有人看过此文件,以及在哪里可以应用此修复程序,我们将不胜感激。
@Override
public void onResume() {
super.onResume();
Timber.v("onResume");
Intent intent = new Intent(context, Token.class);
intent.setAction(ACTION_FETCH_TOKEN);
context.startService(intent);
}
只是为了添加更多上下文,我无法自己重现崩溃。
答案 0 :(得分:1)
在Oreo8 +上,如果未“显示”您的应用程序,则无法启动后台服务,为此,您需要像下面那样编辑代码:
protected void onResume() {
super.onResume(); context.startForegroundService(intent);
}
在服务类的onCreate()方法中,您还需要添加:
*已编辑* 2018-09-18 18:15
PendingIntent pIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("My App")
.setContentText("Doing some work...")
.setContentIntent(pIntent).build();
startForeground(1000, notification);
答案 1 :(得分:0)
我遇到了一个我不想更改为ForegroundService的小型服务的相同问题。然后,我发现Google为在onResume中启动服务提供了此解决方法:
CREATE TRIGGER trigger1
AFTER UPDATE ON `base1`.`a`
FOR EACH ROW
UPDATE `base2`.`b`
SET column_c= NEW.column_c
where id=NEW.id;
CREATE TRIGGER trigger2
AFTER UPDATE ON `base2`.`b`
FOR EACH ROW
UPDATE `base1`.`a`
SET column_b= NEW.column_b
where id=NEW.id;
此问题已在以后的Android版本中解决。
有一种解决方法,可以避免应用程序崩溃。申请可以得到 通过调用Activity.onResume()中的过程状态 ActivityManager.getRunningAppProcesses()并避免在以下情况下启动Service 重要性等级低于 ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND。如果 设备尚未完全唤醒,活动会立即暂停, 最终在完全清醒后再次恢复。