背景
我有一个业余时间的应用程序,已经开发了几年了(在这里),我尽可能处理尽可能多的崩溃(我讨厌看到崩溃报告!)。
问题
最近出现了一个似乎很新的崩溃,就是这个:
android.app.RemoteServiceException:
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1768)
at android.os.Handler.dispatchMessage (Handler.java:106)
at android.os.Looper.loop (Looper.java:164)
at android.app.ActivityThread.main (ActivityThread.java:6494)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:807)
这就是我所见的一切...
似乎只在Android 8.1上发生。由于该应用程序已经相当受欢迎,我非常确定它仍然只会在Android 8.1上发生。
我尝试过的
在网上搜索时,我找到了类似的崩溃情况,但它们都有更多的线索。
所以,我试着回想一下最近的变化。唯一的变化是将支持库迁移到Android-X。其他更改非常微小,我认为它们不可能是原因。
因为我没有更多的线索,所以我决定在 这里 报告关于Android-X部分的问题(如果需要更多信息,请参见那里),因为我不认为这是我尝试解决问题的结果,看起来像是非常特定的情况。
后来,我集成了Firebase Crashlytics,并在每个这样的日志上获得了这个微小的额外线索:
Fatal Exception: android.app.RemoteServiceException
Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=channel_id__app_monitor pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 category=service vis=SECRET)
这很奇怪,因为我正确地打开了服务,否则它根本无法工作。特别奇怪的是,这只会在Android 8.1上发生,而不会在例如相同要求如何启动前台服务的8.0上发生。
有问题的通知是我用来全局监视与应用有关的事件(仅从Android O开始,该版本存在BroadcastReceivers的限制)的前台服务。它在两种情况下更新:
- 语言环境更改
- 勾选出现在某个对话框中(单击对话框后)的复选框。
当我测试了这两种情况时,在Android 8.0和8.1上都可以正常工作。
以下是用于创建/更新通知的代码:
@JvmStatic
fun getUpdatedAppMonitorNotification(context: Context): Notification {
val builder = Builder(context, context.getString(R.string.channel_id__app_monitor)).setSmallIcon(R.drawable.ic_stat_app_icon)//
.setPriority(Notification.PRIORITY_DEFAULT).setCategory(Notification.CATEGORY_SERVICE)
builder.setVisibility(NotificationCompat.VISIBILITY_SECRET)
builder.setShowWhen(false)
if (!PreferenceUtil.getBooleanPref(context, R.string.pref__avoid_showing_app_monitor_notification_dialog, false))
builder.setContentTitle(context.getString(R.string.app_monitor__notification_title))
if (VERSION.SDK_INT < VERSION_CODES.P) {
val mainIntent = Intent(context, MainActivity::class.java)
.putExtra(MainActivity.EXTRA_OPENED_FROM_NOTIFICATION, true)
builder.setContentIntent(PendingIntent.getActivity(context, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT))
} else {
val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS)
.putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
.putExtra(Settings.EXTRA_CHANNEL_ID, context.getString(R.string.channel_id__app_monitor))
val pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
builder.setContentIntent(pendingIntent)
}
return builder.build()
}
我在服务中使用的代码:
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
NotificationId.initNotificationsChannels(this)
val notification = getUpdatedAppMonitorNotification(this)
startForeground(NotificationId.APP_MONITOR, notification)
...
return super.onStartCommand(intent, flags, startId)
}
以下是我从其他地方调用以更新通知的代码:
@JvmStatic
fun updateAppMonitorNotification(context: Context) {
if (VERSION.SDK_INT < VERSION_CODES.O)
return
val notification = AppMonitorService.getUpdatedAppMonitorNotification(context)
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(NotificationId.APP_MONITOR, notification)
}
这些问题
它为什么会发生?是与Android X有关吗?还是通知构建出了问题?
为什么它只在Android 8.1上发生?
R.string.channel_id__app_monitor
提供了翻译,比如说你使用不同的名称创建了通道,然后尝试根据语言环境更改使用它? - CommonsWarechannel_id__app_monitor
没有被翻译,并且也被标记为未翻译。 - android developer