我为android设置了一个警报,它还传递了用于对话框的字符串值。我设置的第一个警报工作,它使用我传递的字符串值。但是对于我设置的以下警报,它仍然使用传递的原始字符串值。
我不确定出了什么问题。这是我的代码:
//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();
}
这是接收器类:
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);
}
}
这是AlertActivity类,其中将出现带有传递值的对话框:
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();
}
}
答案 0 :(得分:0)
对于以下代码行:
PendingIntent pendingAlert = PendingIntent.getBroadcast(MainActivity.this, 0, alertIntent, 0);
对第二个参数使用一些其他int值,否则它将返回相同的待定意图。医生说
PendingIntent返回与之匹配的现有或新PendingIntent 给定参数。仅当FLAG_NO_CREATE已经存在时才可以返回null 提供。
请参阅this了解详情。