使用PendingIntent
时,我们应该通过为setAction
或setData
分配不同的操作来使其唯一(取决于我们的用例),因为系统重用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());
}