afrocode afrocode - 25 days ago 6
Android Question

Android - Passing string values for string in Alert dialog box

I have setup an alarm for android where it also passes string values that is used for the dialog box. The first alarm that I set works, and it uses the string values that I passed. But for the following alarm that I set, it still uses the original string values that was passed.

I'm not really sure what is wrong. Here is my code:

//alert method from the MainActivity
private void setAlert(int alertType, Calendar alertCalendar) {
//identify which alert
switch (alertType) {
case 1:
passFunction = "COURSE START";
passValue = thisTermNum + " is starting on " + oldTermStart;
break;
case 2:
passFunction = "COURSE END";
passValue = thisTermNum + " is ending on " + oldTermEnd;
break;
}

//actual alert
AlarmManager alertManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent alertIntent = new Intent(MainActivity.this, AlertReceiver.class);
Bundle alertBundle = new Bundle();
alertBundle.putString(PASS_ALERT_FUNCTION, passFunction);
alertBundle.putString(PASS_ALERT_VALUE, passValue);
alertIntent.putExtras(alertBundle);
PendingIntent pendingAlert = PendingIntent.getBroadcast(MainActivity.this, 0, alertIntent, 0);
alertManager.set(AlarmManager.RTC_WAKEUP, alertCalendar.getTimeInMillis(), pendingAlert);

Toast.makeText(this, passFunction + " HAS BEEN SET", Toast.LENGTH_SHORT).show();
}


This is the receiver class:

public class AlertReceiver extends WakefulBroadcastReceiver{

//for passed values
String PASS_ALERT_FUNCTION = "PassAlertFunction";
String PASS_ALERT_VALUE = "PassAlertValue";
String receivedFunction;
String receivedValue;


@Override
public void onReceive(Context context, Intent intent) {

//receive passed values
Bundle getAlertExtra = intent.getExtras();
receivedFunction = getAlertExtra.getString(PASS_ALERT_FUNCTION);
receivedValue = getAlertExtra.getString(PASS_ALERT_VALUE);

//for alert sound
Uri alertSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
Ringtone alertTone = RingtoneManager.getRingtone(context, alertSound);
alertTone.play();

//to pass value to AlertActivity for the alert dialog box
Intent passAlertValues = new Intent(context, AlertActivity.class);
Bundle alertBundle = new Bundle();
alertBundle.putString(PASS_ALERT_FUNCTION, receivedFunction);
alertBundle.putString(PASS_ALERT_VALUE, receivedValue);
passAlertValues.putExtras(alertBundle);
passAlertValues.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(passAlertValues);
}
}


This is the AlertActivity class where the dialog box will appear with the passed values:

public class AlertActivity extends AppCompatActivity {

//for passed values
String PASS_ALERT_FUNCTION = "PassAlertFunction";
String PASS_ALERT_VALUE = "PassAlertValue";
String receivedFunction;
String receivedValue;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alert);
setTitle("");

//receive alert values
Intent alertValues = getIntent();
Bundle alertBundle = alertValues.getExtras();
receivedFunction = alertBundle.getString(PASS_ALERT_FUNCTION);
receivedValue = alertBundle.getString(PASS_ALERT_VALUE);

//dialog box for the alert
AlertDialog.Builder alertBuidler = new AlertDialog.Builder(this);
alertBuidler.setTitle(receivedFunction)
.setMessage(receivedValue)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(AlertActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
});
AlertDialog alertDialog = alertBuidler.create();
alertDialog.show();
}
}

Answer

For the following line of code:

PendingIntent pendingAlert = PendingIntent.getBroadcast(MainActivity.this, 0, alertIntent, 0);

Use some other int value for the 2nd parameter, otherwise it will return the same pending intent. The doc says

PendingIntent Returns an existing or new PendingIntent matching the given parameters. May return null only if FLAG_NO_CREATE has been supplied.

Refer this to know more.