我有两项活动。在活动A上,我将闹钟设置为在5分钟后重复。在活动B上,我显示一个带有“贪睡”按钮和“关闭”按钮的对话框。但是,我可以从两个活动中获得相同的PendingIntent。我该如何解决这个问题?
MainActivity.java
public class MainActivity extends Activity {
private static Context context;
public static Context getAppContext(){
return MainActivity.context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);;
MainActivity.context = getApplicationContext();
/** Creating a calendar object corresponding to the date and time set by the user */
for (int i = 0; i < alarm.size(); i++) {
GregorianCalendar calendar1 = new GregorianCalendar(alarm.get(i).getYear(), alarm.get(i).getMonth(), alarm.get(i).getDay(), alarm.get(i).getHour(), alarm.get(i).getMinute());
Intent alarmintent = new Intent("com.med.dic.alarmreceiveactivity");
alarmintent.putExtra("Title", alarm.get(i).getTime());
alarmintent.putExtra("Note", alarm.get(i).getAmount());
int _id = (int) System.currentTimeMillis();
alarmintent.putExtra("Id", _id);
PendingIntent sender = PendingIntent.getActivity(getApplicationContext(), _id, alarmintent, 0);
long cal = calendar1.getTimeInMillis();
/** Setting an alarm, which invokes the operation at alart_time */
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal, 1000*60*1, sender);
}
}
});
}
AlarmReceiveActivity.java
public class AlarmReceiveActivity extends Activity {
private MediaPlayer mAlarmPlayer;
private Calendar calendar = Calendar.getInstance();
private String title;
private String note;
private int id;
private AlarmManager alarmManager;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (extras != null) {
title = extras.getString("Title");
note = extras.getString("Note");
id = extras.getInt("Id");
mAlarmPlayer = MediaPlayer.create(this, R.raw.test_alarm);
mAlarmPlayer.setLooping(true);
mAlarmPlayer.start();
context = MainActivity.getAppContext();
AlertDialog.Builder _builder = new AlertDialog.Builder(AlarmReceiveActivity.this);
_builder.setTitle(title);
_builder.setMessage(note);
_builder.setPositiveButton("Nhắc lại(5')",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mAlarmPlayer.pause();
dialog.dismiss();
});
_builder.setNegativeButton("Bỏ qua",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mAlarmPlayer.pause();
Intent alarmintent = new Intent("com.med.dic.alarmreceiveactivity");
PendingIntent sender = PendingIntent.getActivity(context, id, alarmintent, 0);
alarmManager = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);
alarm.cancel(sender);
finish();
}
});
AlertDialog alert = _builder.create();
alert.setCanceledOnTouchOutside(false);
alert.setCancelable(false);
alert.show();
}
}