从另一个片段中取消片段中的警报和通知集

时间:2015-03-23 08:10:10

标签: android android-fragments android-notifications android-alarms

我正在尝试取消片段中的闹钟设置,来自另一个片段,我该怎么做?

InserisciOra.java (我设置闹钟的片段)

Intent myIntent = new Intent(getActivity(), MyReceiver.class);
if(PendingIntent.getBroadcast(getActivity(), 0, 
    myIntent, 
    PendingIntent.FLAG_NO_CREATE) == null){

        pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent,0);
        alarmManager = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

2 个答案:

答案 0 :(得分:2)

      Intent myIntent = new Intent(getActivity(), MyReceiver.class);
      pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent,0);
      alarmManager = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
      // cancel the alarm
      alarmManager.cancel(pendingIntent);
      // delete the PendingIntent
      pendingIntent.cancel()

答案 1 :(得分:0)

使用cancel方法取消闹钟

Intent myIntent = new Intent(getActivity().,
     MyReceiver.class);

   PendingIntent pendingIntent
    = PendingIntent.getBroadcast(getActivity(),
      0, myIntent, 0);

   AlarmManager alarmManager
     = (AlarmManager)getActivity().getSystemService(ALARM_SERVICE);
    alarmManager.cancel(pendingIntent);