PendingIntent.FLAG_CANCEL_CURRENT如何在警报管理器中工作

时间:2016-03-11 11:53:38

标签: android alarmmanager android-pendingintent alarm

我已经写了一个接收器,想要安排闹钟只能在一定时间内被叫两次

public class AlarmReceiver extends BroadcastReceiver {

private String TAG = AlarmReceiver.class.getName();

/*Get Google tracker Object*/
private Tracker tracker;

private final String DATA_GRAB_TYPE_NIGHT = "night";
private final String DATA_GRAB_DURING_INTERVIEW = "during_interview";

@Override
public void onReceive(final Context context, Intent intent) {
     if(intent.getExtraString("test1").equalsIgnoreCase(DATA_GRAB_TYPE_NIGHT ))
   {
      Log.d("TAG", "NIGHT"); 
        }

    if(intent.getExtraString("test1").equalsIgnoreCase(DATA_GRAB_DURING_INTERVIEW))
   {
      Log.d("TAG", "INTERVIEW"); 
        }
 }


}

这就是我安排闹钟管理器的方式

    Intent nightIntent1 = new Intent(context, AlarmReceiver.class);
    nightIntent1.putExtra("test1", PreferenceKV.TRY_NIGHT_MODE_LOCATION);
    PendingIntent nightPi1 = PendingIntent.getBroadcast(context, 0, nightIntent1, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager nightMngr1 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, 1);
    cal.set(Calendar.HOUR_OF_DAY, 2);

    Log.d(TAG, "Time for Night Invocation setupAlarmManager: " + cal.toString());
    nightMngr1.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), nightPi1);


    Intent nightIntent2 = new Intent(context, AlarmReceiver.class);
    nightIntent2.putExtra("test1", PreferenceKV.TRY_NIGHT_MODE_LOCATION);
    PendingIntent nightPi2 = PendingIntent.getBroadcast(context, 0,          
    nightIntent2, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager nightMngr2 = (AlarmManager) 
    getSystemService(Context.ALARM_SERVICE);

    Calendar cal2 = Calendar.getInstance();
    cal2.add(Calendar.DATE, 2);
    cal2.set(Calendar.HOUR_OF_DAY, 2);
    nightMngr2.set(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), nightPi2);

首先警报会被取消吗? Android如何决定取消以前的报警管理器?

0 个答案:

没有答案