无法展开远程视图 - 错误通知
最近,我收到了越来越多的用户收到RemoteServiceException错误的报告。我每次给出的堆栈跟踪如下所示:
android.app.RemoteServiceException: Bad notification posted from package com.smithyproductions.fasttracks: Couldn't expand RemoteViews for: StatusBarNotification(pkg=com.smithyproductions.fasttracks id=311095 tag=null score=0 notn=Notification(pri=0 contentView=com.smithyproductions.fasttracks/0x7f03007d vibrate=null sound=null defaults=0x0 flags=0x62 kind=[null]) user=UserHandle{0})
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1523)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5341)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:929)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
at dalvik.system.NativeStart.main(NativeStart.java)
我似乎永远无法在HTC Sensation或HTC One X上重现这个问题,这使我相信它可能是特定于设备的。我从以下设备收到此错误的报告:
- 华为 G610 U15
- bq 水瓶座 5 HD
- 恩斯佩尔特楼梯
- JYT JY-G5
...他们都运行Android 4.2.1 - 也许那个版本的Android有一个错误?
我已经在互联网和StackOverflow上搜索了有类似问题的人,尽管大多数时候有很多人遇到这个问题,但它不能正常工作的原因是因为不正确的资源ID或在RemoteViews中使用不受支持的视图。我不明白如果它在98%的设备上工作,它怎么可能是我做错了什么,但我不想告诉这些问题的人,我也无能为力。
就我创建通知的方式而言,我使用 NotificationCompat.Builder 类以及自定义 RemoteViews - 本质上它是一个音乐播放器通知,因此它上面也有一些 ImageButtons。
这是我设置通知的代码:
PendingIntent pi = PendingIntent.getActivity(
getApplicationContext(), 0, new Intent(getApplicationContext(), Showcase.class),
PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews smallContentView = new RemoteViews(getPackageName(),
R.layout.notif_layout);
nowPlayingNotification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(
"Playing: "
+ playbackManager.getCurrentTrack()
.getName()).setContentIntent(pi)
.setOngoing(true)
.setWhen(System.currentTimeMillis())
.setContent(smallContentView).build();
然后我设置了远程视图按钮的所有回调:
Intent playIntent = new Intent(this, RemoteControlReceiver.class);
playIntent.setAction(ACTION_TOGGLE_PLAYBACK);
PendingIntent playPausePendingIntent = PendingIntent.getBroadcast(this,
0, playIntent, 0);
Intent nextIntent = new Intent(this, RemoteControlReceiver.class);
nextIntent.setAction(ACTION_SKIP);
PendingIntent nextPendingIntent = PendingIntent.getBroadcast(this, 0,
nextIntent, 0);
Intent stopIntent = new Intent(this, RemoteControlReceiver.class);
stopIntent.setAction(ACTION_STOP);
PendingIntent stopPendingIntent = PendingIntent.getBroadcast(this, 0,
stopIntent, 0);
nowPlayingNotification.contentView.setTextViewText(R.id.title,
playbackManager.getCurrentTrack().getName());
nowPlayingNotification.contentView.setProgressBar(
android.R.id.progress, 100, mediaProgress, false);
if (notificationIconMixBitmap != null)
nowPlayingNotification.contentView.setImageViewBitmap(R.id.icon,
notificationIconMixBitmap);
else {
nowPlayingNotification.contentView.setImageViewResource(R.id.icon,
R.drawable.notification_icon);
}
nowPlayingNotification.contentView.setOnClickPendingIntent(
R.id.playPause, playPausePendingIntent);
if (playbackManager.getCurrentPlaybackState() == State.Paused) {
nowPlayingNotification.contentView.setImageViewResource(
R.id.playPause, R.drawable.play_icon_solid);
} else {
nowPlayingNotification.contentView.setImageViewResource(
R.id.playPause, R.drawable.pause_icon_solid);
}
if (!playbackManager.isSkipAllowed()) {
nowPlayingNotification.contentView.setImageViewResource(R.id.next,
R.drawable.next_icon_disabled);
} else {
nowPlayingNotification.contentView.setImageViewResource(R.id.next,
R.drawable.next_icon_solid);
}
if (playbackManager.getCurrentPlaybackState() == State.Preparing) {
nowPlayingNotification.contentView.setProgressBar(
android.R.id.progress, 100, mediaProgress, true);
}
nowPlayingNotification.contentView.setOnClickPendingIntent(R.id.next,
nextPendingIntent);
nowPlayingNotification.contentView.setOnClickPendingIntent(R.id.close,
stopPendingIntent);
其次
mNotificationManager.notify(NOTIFICATION_ID, nowPlayingNotification);
如果有人知道任何可能有帮助的东西,也许是4.2.1中公认的错误,那将不胜感激!谢谢