NotificationCompat.Builder addAction - Boolean始终为true

时间:2015-10-30 17:25:30

标签: android

我有以下代码:

public void sendNewNotification (Booking updateBooking) {

    String msg;
    if (updateBooking.getJobStatus () == Booking.eeJob_OnRoute) {
        msg = "Your driver has been dispatched.";
    } else if (updateBooking.getJobStatus () == Booking.eeJob_POB) {
        msg = "Your driver has arrived.";
    } else if (updateBooking.getJobStatus () == Booking.eeJob_Complete) {
        Variables.driverrated = false;
        msg = "Your booking has been completed. Thank you for using " + Variables.setting.getCompanysName () + ".";
    } else if (updateBooking.getJobStatus () == Booking.eeJob_NoShow) {
        msg = "Unfortunately your driver was unable locate you. Please call the office on " + Variables.setting.getCompanytelephone () + ".";
    } else if (updateBooking.getJobStatus () == Booking.eeJob_Cancelled) {
        msg = "Your booking has been cancelled. Please call the office on " + Variables.setting.getCompanytelephone () + " if this is incorrect.";
    } else {
        return;
    }

    PendingIntent detailsIntent =  PendingIntent.getActivity (this, 0, new Intent (this, HomeActivity.class)
            .putExtra ("booking", updateBooking)
            .putExtra ("track", false),
            PendingIntent.FLAG_UPDATE_CURRENT);

    PendingIntent trackingIntent = PendingIntent.getActivity  (this, 0, new Intent (this, HomeActivity.class)
            .putExtra ("booking", updateBooking)
            .putExtra ("track", true),
            PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder (this);
    builder.setAutoCancel (true);
    builder.setDefaults (Notification.DEFAULT_ALL);
    builder.setWhen (System.currentTimeMillis ());
    builder.setContentTitle (Variables.setting.getCompanysName () + " - booking update");
    builder.setSmallIcon (R.drawable.notification);
    builder.setTicker (Variables.setting.getCompanysName () + " - booking update");
    builder.setPriority (NotificationCompat.PRIORITY_HIGH);
    builder.setContentText (msg);
    builder.setContentIntent (trackingIntent);

    builder.addAction (R.drawable.documents_32px, "DETAILS", detailsIntent);

    if (updateBooking.getJobStatus () != Booking.eeJob_Complete) {
        builder.addAction (R.drawable.notification, "TRACK", trackingIntent);
    } else {
        builder.addAction (R.drawable.profile_32px, "RATE", detailsIntent);
    }

    NotificationManager manager = (NotificationManager) getSystemService (NOTIFICATION_SERVICE);
    manager.notify (getTaskId (), builder.build ());

}

我的问题在于我的onCreate函数我有这两行:

m_updateBooking = (Booking) getIntent ().getSerializableExtra ("booking");
m_TrackDriver = (Boolean) getIntent ().getSerializableExtra ("track");

m_updateBooking完美无瑕。但是m_TrackDriver总是如此。无论我按什么按钮。谁能解释为什么会这样?

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

由于Intent PendingIntentrequestCode的操作具有相同的操作,因此您需要指定其他FLAG_UPDATE_CURRENT,否则请{em}其他<{1}} PendingIntent中的{<1>}中的新 extras 将替换为Intent。出于这个原因,您始终true,因为它位于请求的最后PendingIntent

StackOveflow answer详细说明了PendingIntent的工作原理。

要解决您的问题,请更改以下代码:

PendingIntent detailsIntent =  PendingIntent.getActivity (this, 0, new Intent (this, HomeActivity.class)
        .putExtra ("booking", updateBooking)
        .putExtra ("track", false),
        PendingIntent.FLAG_UPDATE_CURRENT);

PendingIntent trackingIntent = PendingIntent.getActivity  (this, 1, new Intent (this, HomeActivity.class)
        .putExtra ("booking", updateBooking)
        .putExtra ("track", true),
        PendingIntent.FLAG_UPDATE_CURRENT);