我在 Activity 的 onResume 上启动了一项服务,它在 Oreo 上运行良好,但最近我看到 Android P 崩溃,显示“无法恢复 Activity.... 不允许启动服务意图... 应用程序在后台。 .”。有没有人以前见过这个并且能够应用修复,任何输入将不胜感激。
@Override
public void onResume() {
super.onResume();
Timber.v("onResume");
Intent intent = new Intent(context, Token.class);
intent.setAction(ACTION_FETCH_TOKEN);
context.startService(intent);
}
只是为了添加更多上下文,我自己无法重现崩溃。
在 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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)