2016-04-05 114 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(); 
     } 
    }