我们调整了针对Oreo的持续通知,效果很好。现在,仅在Pie(不是在Oreo设备上发生)上,我们得到了标题错误。我缺少的Pie中的前台服务是否有所更改?
这是前台服务的onCreate代码->
override fun onCreate() {
super.onCreate()
val notification: Notification = NotificationCompat.Builder(this, packageName)
.setSmallIcon(R.drawable.status_notification_icon)
.setContentTitle(getString(R.string.ongoing_notify_temp_title))
.setContentText(getString(R.string.ongoing_notify_temp_message))
.setGroup(AppConstants.NOTIFICATION_GROUP_ONGOING)
.setColor(ContextCompat.getColor(this, R.color.custom_blue))
.build()
startForeground(ONGOING_NOTIFY_ID, notification)
appSettings = AppSettings(this)
weatherLookUpHelper = WeatherLookUpHelper()
MyRoomDatabase.getInstance().invalidationTracker.addObserver(onChange)
retrieveCurrentLocation()
createAlarmManager()
}
如您所见,我们只是创建通知,然后调用startForeground。关于为什么此代码会产生标题错误的任何想法?
侧面说明:Fabric Crashlytics显示此崩溃仅在运行Pie的像素设备(像素,像素xl,像素2,像素2 xl)上发生
编辑:我们的清单中确实有前台许可
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
答案 0 :(得分:2)
如此处Background Service Limitations所述,该应用程序/服务有五秒钟的时间来调用startForeground()
,如果该应用程序在该时限内未调用startForeground(),则系统将停止该服务并声明该应用程序成为ANR。
有一些可能性:
startForeground()
方法之前,您的前台服务都会被破坏/完成。onCreate
方法,而将调用onStartCommand
。然后将您的逻辑移至onStartCommand
,以调用startForeground()
方法。startForeground
must not be 0中的通知ID,否则也会导致崩溃。答案 1 :(得分:1)
我缺少的Pie中的前台服务发生了什么变化?
是 在这里migration notes of Android 9 / Pie
更改
前台服务许可
摘要
想要使用前台服务的应用程序现在必须首先请求FOREGROUND_SERVICE权限。这是普通权限,因此系统会自动将其授予请求的应用程序。未经许可启动前台服务会引发SecurityException。
更新
Google问题追踪器Context.startForegroundService() did not then call Service.startForeground 中的相关问题
答案 2 :(得分:0)
在ONCREATE
中实现以下代码后,我停下来获取错误消息
Intent intent1 = new Intent(this, YourActivity.class);
try {
startService(intent1);
}catch ( Exception e1){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
this.startForegroundService(intent1);
}else {
Crashlytics.log("crash for first time, trying another.");
this.startService(intent1);
}
}
对我来说听起来很奇怪,但是可以正常工作而不会崩溃。