问题
Lately I've been receiving more and more reports of users getting a RemoteServiceException error. The stack trace i'm given every time is as follows:
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)
I can never seem to reproduce the issue myself on a HTC Sensation or HTC One X which leads me to believe it may be device specific. I've received reports of this error from the following devices:
- Huawei G610 U15
- bq Aquaris 5 HD
- Enspert Stairway
- JYT JY-G5
...and they're all running Android 4.2.1 - maybe there's a bug with that version of Android?
I've scoured the internet and StackOverflow for people with similar issues and although there are a lot of people with this issue most of the time the reason it wasn't working properly for them was because of incorrect resource ids or using unsupported Views in the RemoteViews. I don't understand how it could be something I'm doing wrong if it works on 98% of devices but I don't want to tell these people with the issue that there's nothing I can do about it either.
In terms of the way I create the notification, I use the NotificationCompat.Builder class along with a custom RemoteViews - essentially it's a music player notification so it has some ImageButtons on it too.
Here's my code where I setup the notification:
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();
and then I set all the callbacks for the RemoteViews buttons:
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);
followed by
mNotificationManager.notify(NOTIFICATION_ID, nowPlayingNotification);
If anyone knows anything that could help, maybe a recognised bug in 4.2.1, it would be much appreciated! Thanks
回答1:
I saw similar issues in my builds - Only Kitkat Devices are throwing these in large numbers - Android OS version: 4.4.4, 4.4.2, 4.2.2, 4.0.4
Fixed by changing the drawable used by Notification .
.addAction(R.drawable.ic_forward_black_24dp, VIEW_ACTION, pendingIntentView);
We stopped using Vector Assets and started using Image Assets.
R.drawable.ic_forward_black_24dp no longer uses xml file (Vector Asset) and instead we now use png file (Image Asset).
回答2:
I had the same issue with my phone.
you create notification with below code :
nowPlayingNotification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(
"Playing: "
+ playbackManager.getCurrentTrack()
.getName()).setContentIntent(pi)
App crashes if your pending intent -> pi specifying intent to be launched is set to null as below :
PendingIntent pi= PendingIntent.getActivity(
getApplicationContext(),
0,
***null***,
0
);
replace null with new Intent() and test if solves the issue.
On Gingerbread .setcontentIntent(intent) is mandatory otherwise IllegalArgumentException will be thrown. But in Jellybean no exception is thrown if we remove .setcontentIntent(intent)
来源:https://stackoverflow.com/questions/22614912/couldnt-expand-remoteviews-bad-notification