PendingIntent BroadcastReceiver获得null附加功能

时间:2014-10-18 18:17:07

标签: android android-intent android-pendingintent

使用PendingIntent时,我们应该通过为setActionsetData分配不同的操作来使其唯一(取决于我们的用例),因为系统重用Intents并进行比较他们使用Intent.filterEquals 比较意图附加内容,但是对于我Intent中收到的第一个Intents和后续BroadcastReceiver im我把额外的设置为null,除非当然我取消注释行设置意图data uri这是预期的行为,如果是这样,它在某处记录或者这是一个错误?

 public static void setOutgoingNotification(Context context,
                                           CharSequence contentTitle,
                                           CharSequence contentText,
                                           boolean available) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

    builder.setSmallIcon(R.drawable.abc_ab_share_pack_holo_dark)
        .setContentTitle(contentTitle)
        .setContentText(contentText)
        .setOngoing(true);

    NotificationCompat.Action action;

    Intent availabilityIntent = new Intent(context, AvailabilityReceiver.class);
    availabilityIntent.putExtra(AvailabilityReceiver.EXTRA_AVAILABILITY, !available);
    //availabilityIntent.setData(Uri.parse(availabilityIntent.toUri(Intent.URI_INTENT_SCHEME)));

    PendingIntent availabilityPendingIntent = PendingIntent.getBroadcast(context, 0, availabilityIntent,
        PendingIntent.FLAG_UPDATE_CURRENT);


    if(available){
        action = new NotificationCompat.Action(R.drawable.ic_stat_off,
            context.getString(R.string.unavailable),
            availabilityPendingIntent);
    }else {
        action = new NotificationCompat.Action(R.drawable.ic_stat_on,
            context.getString(R.string.available),
            availabilityPendingIntent);
    }

    builder.addAction(action);

    NotificationManager mgr = (NotificationManager)context
        .getSystemService(Context.NOTIFICATION_SERVICE);


    mgr.notify(ONGOING_NOTIFICATION, builder.build());
}

0 个答案:

没有答案