roa*_*ter 15 java notifications android remoteview
最近我收到越来越多的用户收到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)
Run Code Online (Sandbox Code Playgroud)
我似乎永远无法在HTC Sensation或HTC One X上重现这个问题,这让我相信它可能是特定于设备的.我从以下设备收到了此错误的报告:
...而且他们都在运行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();
Run Code Online (Sandbox Code Playgroud)
然后我设置RemoteViews按钮的所有回调:
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);
Run Code Online (Sandbox Code Playgroud)
其次是
mNotificationManager.notify(NOTIFICATION_ID, nowPlayingNotification);
Run Code Online (Sandbox Code Playgroud)
如果有人知道任何可以帮助的事情,也许是4.2.1中公认的错误,我将不胜感激!谢谢
小智 1
我的手机也有同样的问题。
您使用以下代码创建通知:
nowPlayingNotification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(
"Playing: "
+ playbackManager.getCurrentTrack()
.getName()).setContentIntent(pi)
Run Code Online (Sandbox Code Playgroud)
如果您的挂起意图 -> 指定要启动的意图的 pi 设置为 null,则应用程序崩溃,如下所示:
PendingIntent pi= PendingIntent.getActivity(
getApplicationContext(),
0,
***null***,
0
);
Run Code Online (Sandbox Code Playgroud)
将 null 替换为 new Intent() 并测试是否解决了问题。
在 Gingerbread 上 .setcontentIntent(intent) 是强制性的,否则将抛出 IllegalArgumentException。但在 Jellybean 中,如果我们删除 .setcontentIntent(intent) ,则不会引发异常
| 归档时间: |
|
| 查看次数: |
9471 次 |
| 最近记录: |