希望有人可以帮助解决一个奇怪的错误:当我们的 Android 应用程序发布状态栏通知并且用户正在收听 Pandora 时,通知的音量会降低(如预期的那样),但直到下一首歌曲才会恢复。
这似乎不会发生在 Pandora 以外的流媒体应用程序或我们以外的应用程序中——尽管据我所知,我们对通知 API 的使用是标准的。它也仅限于某些设备:它不会发生在我们的 Nexus 4 或 Droid RAZR M 上,但我们可以在运行 4.2.2 的 Galaxy Nexus 上始终如一地重现它。
知道发生了什么吗?
编辑 根据要求,我们的 BroadcastReceiver 代码的一些信息。最初,它通过 ContentProvider 对我们的 DB 做了很多事情,以获取要在通知中显示的信息。完成后:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notification_new_message)
.setTicker(Html.fromHtml("<strong>" + senders[0] + "</strong> " + summaries[0]))
.setContentInfo((messageCount > 1) ? Integer.toString(messageCount) : "").setAutoCancel(true);
// Notification sound and vibration
if (prefs.getBoolean(PREF_SOUND, true)) {
setupSound(builder);
}
// Blinky light
if (prefs.getBoolean(PREF_BLINK, true)) {
setupLight(builder, LIGHT_COLOR);
}
Intent resultIntent;
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
if (senders.length > 1) {
String contentTitle = messageCount + " new messages";
builder.setContentTitle(contentTitle);
builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher));
NotificationCompat.InboxStyle bigStyle = new NotificationCompat.InboxStyle();
bigStyle.setBigContentTitle(contentTitle);
HashSet<String> uniqueSenders = new HashSet<String>(senders.length);
for (i = 0; i < senders.length; i++) {
bigStyle.addLine(Html.fromHtml("<strong>" + senders[i] + "</strong> " + summaries[i]));
uniqueSenders.add(senders[i]);
}
builder.setContentText(TextUtils.join(", ", uniqueSenders));
builder.setStyle(bigStyle);
resultIntent = new Intent(App.getContext(), ChatListActivity.class);
} else {
builder.setContentTitle("Message from " + senders[0]);
setLargeImage(context, lastCorrespondents, builder);
builder.setContentText(summaries[0]);
// Intent + back stack
resultIntent = new Intent(context, ChatActivity.class);
resultIntent.putExtra(MessageDbContract.KEY_CORRESPONDENTS, lastCorrespondents);
taskStackBuilder.addParentStack(ChatActivity.class);
}
resultIntent.putExtra(Constants.INTENT_KEY_CALLER, getClass().getSimpleName());
// Finish configuring action
invokeNotificationManager(context, builder, resultIntent, taskStackBuilder, NOTIFICATION_ID_NEW_MESSAGES);
以及上面引用的辅助函数:
private void setLargeImage(Context context, String lastCorrespondents, NotificationCompat.Builder builder) {
// Set large image
try {
Uri imageUri = Util.getThumbnailUriForCorrespondents(context, lastCorrespondents);
Bitmap largeImage = MediaStore.Images.Media.getBitmap(context.getContentResolver(), imageUri);
builder.setLargeIcon(largeImage);
} catch (IOException e) {
Util.log(this, "Encountered IOException when setting large icon for new message notification.");
}
}
private void setupLight(NotificationCompat.Builder builder, int lightColor) {
builder.setLights(lightColor, LIGHT_ON_DURATION, LIGHT_OFF_DURATION);
}
private void setupSound(NotificationCompat.Builder builder) {
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(uri);
builder.setVibrate(VIBRATION_PATTERN);
}
private void invokeNotificationManager(Context context, NotificationCompat.Builder builder, Intent resultIntent,
TaskStackBuilder taskStackBuilder, int notificationId) {
taskStackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = taskStackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
// Get notification manager, create notification, and notify.
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(App.NOTIFICATION_NEW_MESSAGE, notificationId, builder.build());
}