2016-04-05 98 views
-3

我正在使用Android上的本地通知。通知是基于给定时间发送的。停止发送本地通知android

用户可以使用Switch控制此通知(如果它是ON - >通知将被发送,并且如果它是OFF->该通知必须被删除“NOt Only Canceled”并且根本不被发送) 。 ON案件完美无缺,但对于OFF案件,通知仍会发送。

任何人都可以帮助“停止”发送通知时,开关设置为OFF?

这里是我使用的开关: enter image description here

这是我使用的代码:

if(isChecked == true) 
    { 
     // Send the Notification 

} else { 

     // Stop Sending the Notification 

    } 
+0

你能否提供更多的信息,例如,你在哪里有你的代码,你已经尝试过? –

+0

这可能会帮助你,但我不确定。参考:http://stackoverflow.com/questions/36347187/how-to-stop-alarmmanager-when-activity-start/36349036#36349036 – Sabari

+0

@Sabari非常感谢你。这正是我正在寻找的。你能补充说这个问题的答案吗? – Ghadeer

回答

0

这可能会帮助你,但我不能肯定。

编号:How to stop AlarmManager when activity start

要取消通知:

public void cancelNotification(int requestCode) { 
     try { 
      Intent notificationIntent = new Intent(getApplicationContext(), NotificationReceiver.class); 
      PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
      AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE); 
      alarmManager.cancel(pendingIntent); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    }