2014-11-01 96 views
0

我創建了一個通知與延遲發射,進行與Handler.postdelay停止運行與postdelay(安卓)處理

現在,我希望我的用戶能夠在某處停止這30秒的運行處理過程:

handler.postDelayed(new Runnable() { 
     public void run() { 
      NotificationCompat.Builder mBuilder = 
        new NotificationCompat.Builder(Inlopen.this) 
          .setSmallIcon(R.drawable.iconsmall) 
          .setContentTitle("U moet uw voeten controleren!") 
          .setContentText("Uw moet uw voeten controleren!"); 
      Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
      mBuilder.setSound(alarmSound); 
      NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
      notificationManager.notify(1, mBuilder.build()); 

     } 
    }, 30000); 

如何做到這一點? 我已經搜索了很多,但我無法找到如何做到這一點..

回答

0

您可以使用Handler.removeCallbacks(Runnable)。最好的選擇是創建一個固定的Runnable對象,並在使用Handler.postDelayed(Runnable, long)時提供它,因爲您將能夠從隊列中專門刪除所述Runnable。

// create the Runnable object 
private final static Runnable NOTIF = new Runnable() { 

    @Override 
    public void run() { 
     // TODO notification code goes here... 
    }; 

}; 

// then use it 
handler.removeCallbacks(NOTIF); // to remove any posts of NOTIF 
handler.postDelayed(NOTIF, 30000); // to post NOTIF with a delay of 30 seconds 
+0

我該在哪裏放置它? – user2971732 2014-11-01 15:40:11

+0

將Runnable與其他變量一起放入。這些電話本身可以在任何你想要的地方發生,只要處理器準備好搖擺和/或滾動。 – 2014-11-01 15:48:54

+0

謝謝!通過你的代碼管理它的工作! – user2971732 2014-11-01 15:54:01

0

你必須致電handler.removeCallbacks(null)。通過這種方式,所有排隊的runnbable將被刪除