为什么在某些设备上创建通知时会收到RemoteServiceException?

时间:2018-02-11 16:22:04

标签: android android-notifications

当我的应用创建通知时,其主要活动消失而没有任何痕迹。这可能只是一个常规的未捕获异常,因为我不确定此特定设备在这种情况下是否会显示错误消息。

此问题仅发生在运行Android 5.1.1的华为Y560-L01上。我自己无法访问设备,这使调试变得棘手。我无法在配置了类似规格的模拟器上重现它并运行Lollipop(5.1)。

以下是我创建通知的方式:

private static final int NOTIFICATION_ID = 1;

private final PlaybackService service; // Inherits from Service
private Bitmap logoBitmap; // Initialized before the notification is created

private void someMethod() {
    ...
    service.startForeground(NOTIFICATION_ID, createNotification());
    ...
}

private Notification createNotification() {
    PendingIntent contentIntent = TaskStackBuilder.create(service)
        .addParentStack(MainActivity.class)
        .addNextIntent(new Intent(service, MainActivity.class))
        .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    PendingIntent stopIntent = service.createPendingIntentForAction(PlaybackService.STOP_ACTION);
    PendingIntent pauseIntent = service.createPendingIntentForAction(PlaybackService.PAUSE_ACTION);
    PendingIntent playIntent = service.createPendingIntentForAction(PlaybackService.PLAY_ACTION);

    NotificationCompat.MediaStyle mediaStyle = new NotificationCompat.MediaStyle()
        .setShowActionsInCompactView(0 /* Play/Pause */, 1 /* Stop */)
        .setShowCancelButton(true)
        .setCancelButtonIntent(stopIntent);

    int smallIcon = player.getState() == PlayerState.PLAYING ? R.drawable.ic_status_bar_play : R.drawable.ic_status_bar_pause;
    Bitmap largeIcon = logoBitmap;

    NotificationCompat.Action playOrPauseAction;
    if (player.getState() == PlayerState.PLAYING) {
        playOrPauseAction = new NotificationCompat.Action(R.drawable.ic_pause, "Pause", pauseIntent);
    } else {
        playOrPauseAction = new NotificationCompat.Action(R.drawable.ic_play, "Play", playIntent);
    }
    NotificationCompat.Action stopAction = new NotificationCompat.Action(R.drawable.ic_stop, "Stop", stopIntent);

    return new NotificationCompat.Builder(service.getApplicationContext())
        .setContentTitle(player.getStation().getName())
        .setContentText(player.getCurrentTrack())
        .setSmallIcon(smallIcon)
        .setLargeIcon(largeIcon)
        .setCategory(NotificationCompat.CATEGORY_SERVICE)
        .setPriority(NotificationCompat.PRIORITY_LOW)
        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
        .setOngoing(true)
        .setContentIntent(contentIntent)
        .addAction(playOrPauseAction)
        .addAction(stopAction)
        .setStyle(mediaStyle)
        .build();
}

这是导致应用程序死亡的RemoteServiceException

D/AndroidRuntime(14550): Shutting down VM
E/AndroidRuntime(14550): FATAL EXCEPTION: main
E/AndroidRuntime(14550): Process: com.frozenfractal.radio, PID: 14550
E/AndroidRuntime(14550): android.app.RemoteServiceException: Bad notification posted from package com.frozenfractal.radio: Couldn't expand RemoteViews for: StatusBarNotification(pkg=com.frozenfractal.radio user=UserHandle{0} id=1 tag=null score=-10 key=0|com.frozenfractal.radio|1|null|10174: Notification(pri=-1 contentView=com.frozenfractal.radio/0x109007f vibrate=null sound=null defaults=0x0 flags=0x62 color=0x00000000 category=service actions=2 vis=PUBLIC))
E/AndroidRuntime(14550):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1480)
E/AndroidRuntime(14550):        at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(14550):        at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime(14550):        at android.app.ActivityThread.main(ActivityThread.java:5298)
E/AndroidRuntime(14550):        at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(14550):        at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime(14550):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:911)
E/AndroidRuntime(14550):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)
I/Process (14550): Sending signal. PID: 14550 SIG: 9

但是,StatusBar(一个不同的流程)中出现android.widget.RemoteViews$ActionException类型的错误,我认为这可能更相关:

E/StatusBar( 3283): couldn't inflate view for notification com.frozenfractal.radio/0x1
E/StatusBar( 3283): android.widget.RemoteViews$ActionException: view: android.view.ViewStub doesn't have method: setTextColor(int)
E/StatusBar( 3283):     at android.widget.RemoteViews.getMethod(RemoteViews.java:776)
E/StatusBar( 3283):     at android.widget.RemoteViews.access$300(RemoteViews.java:75)
E/StatusBar( 3283):     at android.widget.RemoteViews$ReflectionAction.apply(RemoteViews.java:1285)
E/StatusBar( 3283):     at android.widget.RemoteViews.performApply(RemoteViews.java:2677)
E/StatusBar( 3283):     at android.widget.RemoteViews.apply(RemoteViews.java:2637)
E/StatusBar( 3283):     at com.android.systemui.statusbar.BaseStatusBar.inflateViews(BaseStatusBar.java:1201)
E/StatusBar( 3283):     at com.android.systemui.statusbar.BaseStatusBar.createNotificationViews(BaseStatusBar.java:1415)
E/StatusBar( 3283):     at com.android.systemui.statusbar.phone.PhoneStatusBar.addNotification(PhoneStatusBar.java:1660)
E/StatusBar( 3283):     at com.android.systemui.statusbar.BaseStatusBar$4$2.run(BaseStatusBar.java:300)
E/StatusBar( 3283):     at android.os.Handler.handleCallback(Handler.java:739)
E/StatusBar( 3283):     at android.os.Handler.dispatchMessage(Handler.java:95)
E/StatusBar( 3283):     at android.os.Looper.loop(Looper.java:135)
E/StatusBar( 3283):     at android.app.ActivityThread.main(ActivityThread.java:5298)
E/StatusBar( 3283):     at java.lang.reflect.Method.invoke(Native Method)
E/StatusBar( 3283):     at java.lang.reflect.Method.invoke(Method.java:372)
E/StatusBar( 3283):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:911)
E/StatusBar( 3283):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)
I/StatusBar( 3283): pkg com.frozenfractal.radio block 6
W/StatusBar( 3283): removeNotification for unknown key: 0|com.frozenfractal.radio|1|null|10174
D/PhoneStatusBar( 3283): mNotificationData.hasActiveClearableNotifications()=false
D/NotificationService(  953): onNotification error pkg=com.frozenfractal.radio tag=null id=1; will crashApplication(uid=10174, pid=14550)

ViewStub没有setTextColor(int)方法,这是正确的,所以我猜测这里不应该有ViewStub,但是而是(膨胀的)TextView或类似的。

我潜入了Android源代码,但它并没有真正启发。 RemoteViews$ActionException被抛出here。这由BaseStatusBar here捕获并记录。

可能来自build.gradle的相关内容(我知道其中一些已经过时了,但升级后给了我一大堆编译错误,我现在还没有时间):

minSdkVersion 16
targetSdkVersion 24
compileSdkVersion 24
buildToolsVersion "24.0.1"

dependencies {
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'com.android.support:design:24.2.0'
}

StackOverflow上有很多问题询问RemoteServiceException,但大多数问题都没有显示StatusBar进程的任何输出,所以很难说他们是一样的。无论如何,大多数都没有得到答复。只有this似乎在评论中有可能的解决方法。我还发现this bug report用于不同的应用程序,它显示相同的堆栈跟踪,并且也在华为设备上。它显然是通过系统更新得到修复的,因此改变可能不完全是我自己的错。

如果我可以访问该设备,我会尝试简化通知直到问题消失,但遗憾的是我没有那么奢侈。有没有人看到我的代码有任何(可能)错误?或者知道如何更改代码以在模拟器上重现问题?

0 个答案:

没有答案