我使用Handler.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 :(得分:0)
你必须致电handler.removeCallbacks(null)
。这样就可以删除所有排队的runnbable
答案 1 :(得分: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