处理程序不取消Runnables(Android)

时间:2014-11-02 13:26:13

标签: android

我制作了一个简单的应用程序,可以在用户必须检查时通知用户。

我设法以正确的延迟发送通知,但我也希望用户能够在延迟之间的某处关闭通知'束'(通过交换机)。 我想用Handler.removeCallBacks做这件事,但它没有完成这项工作。

final Switch mySwitch = (Switch) findViewById(R.id.theswitch);
        mySwitch.setChecked(myPrefs.getBoolean("switch", false));





        //attach a listener to check for changes in state
        mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         final boolean isChecked) {


                editor.putBoolean("switch", isChecked);
                editor.commit();

                if (isChecked) {
                    //switch has been switched ON

                    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+1:00"));
                    Date currentLocalTime = cal.getTime();
                    DateFormat date = new SimpleDateFormat("HH:mm");
// you can get seconds by adding  "...:ss" to it
                    date.setTimeZone(TimeZone.getTimeZone("GMT+1:00"));
                    String localTime = date.format(currentLocalTime);

                    Log.w(localTime, "1");


                    if (localTime.equals("08:50") || localTime.equals("08:51") || localTime.equals("08:52") || localTime.equals("08:53") || localTime.equals("08:54") || localTime.equals("08:55") || localTime.equals("08:56")
                            || localTime.equals("08:57") || localTime.equals("08:58") || localTime.equals("08:59") || localTime.equals("09:00") || localTime.equals("09:01") || localTime.equals("09:02") || localTime.equals("09:03")
                            || localTime.equals("09:04") || localTime.equals("09:05") || localTime.equals("09:06") || localTime.equals("09:07") || localTime.equals("09:08") || localTime.equals("09:09") || localTime.equals("09:10")
                            || localTime.equals("09:11") || localTime.equals("09:12") || localTime.equals("09:13") || localTime.equals("09:14") || localTime.equals("09:15") || localTime.equals("09:16") || localTime.equals("09:17")
                            || localTime.equals("09:18") || localTime.equals("09:19") || localTime.equals("09:20") || localTime.equals("09:21") || localTime.equals("09:22") || localTime.equals("09:23") || localTime.equals("09:24")
                            || localTime.equals("09:25") || localTime.equals("09:26") || localTime.equals("09:27") || localTime.equals("09:28") || localTime.equals("09:29") || localTime.equals("09:30") || localTime.equals("09:31")
                            || localTime.equals("09:32") || localTime.equals("09:33") || localTime.equals("09:34") || localTime.equals("09:35") || localTime.equals("09:36") || localTime.equals("09:37") || localTime.equals("09:38")
                            || localTime.equals("09:39") || localTime.equals("09:40") || localTime.equals("09:41") || localTime.equals("09:42") || localTime.equals("09:43") || localTime.equals("09:44") || localTime.equals("09:45")
                            || localTime.equals("09:46") || localTime.equals("09:47") || localTime.equals("09:48") || localTime.equals("09:49") || localTime.equals("09:50") || localTime.equals("09:51") || localTime.equals("09:52")
                            || localTime.equals("09:53") || localTime.equals("09:54") || localTime.equals("09:55") || localTime.equals("09:56") || localTime.equals("09:57") || localTime.equals("09:58") || localTime.equals("09:59")
                            || localTime.equals("10:00") || localTime.equals("10:01") || localTime.equals("10:02") || localTime.equals("10:03") || localTime.equals("10:04") || localTime.equals("10:05") || localTime.equals("10:06")
                            || localTime.equals("10:07") || localTime.equals("10:08") || localTime.equals("10:09") || localTime.equals("10:10") || localTime.equals("14:16")) {

                        Log.w("It is between", "those times");


                        boolean beenfired2 = myPrefs.getBoolean("beenfired", false);


                        if (beenfired2 != true) {

                            Log.w("yes", "it is not equal to true");
                            // day 1
                            mHandler.postDelayed(mClickRunnable, 36000);
                            mHandler.postDelayed(mClickRunnable, 72000);


                            beenfired = true;
                            editor.putBoolean("beenfired", beenfired);
                            editor.commit();


                        } else {

                            Log.w("else", "else");


                        }

                    } else {

                        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Inlopen.this);

                        // set title
                        alertDialogBuilder.setTitle("Alarm kan niet worden ingeschakeld");

                        // set dialog message
                        alertDialogBuilder
                                .setMessage("U kunt het alarm alleen inschakelen tussen 9:00 en 10:00 's ochtends.")
                                .setCancelable(false)

                                .setNegativeButton("Oke", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        // if this button is clicked, just close
                                        // the dialog box and do nothing


                                        mySwitch.setChecked(false);
                                        editor.putBoolean("switch", false);
                                        editor.commit();
                                        dialog.cancel();
                                    }
                                });

                        // create alert dialog
                        AlertDialog alertDialog = alertDialogBuilder.create();

                        // show it
                        alertDialog.show();
                    }


                } else {
                    //switch has been switched OFF
                    beenfired = false;

                    mHandler.removeCallbacks(mClickRunnable);

                    editor.putBoolean("beenfired", beenfired);
                    editor.commit();

                    editor.putBoolean("switch", false);
                    editor.commit();


                }

            }
        });


    }


    private Handler mHandler = new Handler();
    private Runnable mClickRunnable = new Runnable() {
        @Override
        public void run() {
            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(Inlopen.this)
                            .setSmallIcon(R.drawable.iconsmall)
                            .setContentTitle("DM Voet App")
                            .setContentText("Uw moet uw voeten controleren!");
            mBuilder.setAutoCancel(true);
            Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            mBuilder.setSound(alarmSound);
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(1, mBuilder.build());
        }
    };

1 个答案:

答案 0 :(得分:0)

我在停止处理程序时始终将这两种方法结合起来:

mHandler.removeCallbacks(mClickRunnable);
mHandler.removeMessages(0);

说实话,我真的不明白它们之间的区别,但它对我有用。 (如果我删除其中任何一个,它将无法工作)