应用程序通知会破坏Pandora卷

时间:2013-07-16 00:57:56

标签: android audio notifications

希望有人可以帮助解决一个奇怪的错误:当我们的Android应用发布状态栏通知并且用户正在收听Pandora时,音量会下降以获得通知(正如预期的那样)但是直到下一首歌才会回来

除了Pandora之外的流媒体应用程序或者我们以外的应用程序似乎不会发生这种情况 - 尽管据我所知,我们对通知API的使用是标准的。它也仅限于某些设备:它不会发生在我们的Nexus 4或Droid RAZR M上,但我们可以在运行4.2.2的Galaxy Nexus上一致地重现它。

知道发生了什么事吗?

修改 根据要求,我们的BroadcastReceiver代码有一些信息。最初,它通过ContentProvider在我们的数据库中执行大量操作,以获取要在通知中显示的信息。一旦完成:

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());
}

0 个答案:

没有答案