2017-05-17 91 views
0

我想通知我取消它自己的重複報警。但是,我不知道如何在創建之前傳遞pendingIntent(稍後取消它)。Android:通知取消它重複報警

報警設置在這裏:

public class Schedule extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 
     PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, ""); 
     wakeLock.acquire(); 

     //next is notification code. // 

     ... 

     //create intent. 
     Intent notificationIntent = new Intent(context, MainActivity.class); 

     PendingIntent contentIntent = PendingIntent.getActivity(context, 
       NOTIFY_ID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

     ... 

     //// 
     wakeLock.release(); 
    } 

    public void setAlarm(Context context) { 

     ... 

     Intent intent = new Intent(context, Schedule.class); 
     intent.putExtra("DELAY", delay); 
     intent.putExtra("NOTIFICATION_ID", id); 
     intent.putExtra("TITLE_TEXT", titleText); 
     intent.putExtra("BIG_TEXT", bigText); 

     PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1000 * 60 * delay, 1000 * 60 * delay, pendingIntent); 
    } 
} 

關鍵的問題是,我想取消通知單擊(onRecieve方法意圖)的重複報警,但如果取消報警需要的PendingIntent,該通知的一部分。任何方式來欺騙系統?

回答

1

使用AlarmManager.cancel()cancel具體的alarm和使用相同的PendingIntent用於創建警報。當然,你應該使用之前使用的requestCodeNOTIFY_ID)。

試試這個:

Intent intent = new Intent(this, Schedule.class); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, NOTIFY_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 

alarmManager.cancel(pendingIntent); 

添加該代碼部分到您的MainActivty的onCreate()方法,您notification將從通知面板意圖就可以了MainActivityclick

希望這會有所幫助〜

+0

你好再次:)我看到了這個答案已經在這裏(當然,我試圖谷歌它之前問)。但問題是,方法cancel()需要在通知傳遞給它之後創建的pendingIntent。或者我可以簡單地創建具有相同ID的pendingIntent以便能夠取消它?如果是這樣,這是奇妙的。我會盡快嘗試,非常感謝你:) – Cakeee

+0

這是行不通的:(我按照你說的做了,並且通知在點擊後繼續顯示。 – Cakeee

+0

確保你使用相同的通知ID取消通知。你是否想刪除通過面板通知時點擊通知? – FAT