handler.removeCallbacksAndMessages 不工作

handler.removeCallbacksAndMessages not working

我的应用有一个注册服务 ScreenStateReceiver,它会在屏幕打开时创建一个处理程序,并在屏幕关闭时删除它。

我的问题是 handler.removeCallbacksAndMessages 在屏幕关闭时不起作用。我知道这一点,因为每当屏幕打开时,我都会从创建的所有处理程序中获取多个日志。

我的代码:

public class mainActivity extends ActionBarActivity {

static Button setButton;
static Context activityContext;

static SharedPreferences preferences;
static SharedPreferences.Editor editor;

static long handlerDelay;
static Handler handler;
static Runnable notificationRunnable;
static Runnable lockRunnable;
static String handlerType;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setButton = (Button) findViewById(R.id.setButton);
    activityContext = this;

    preferences = getSharedPreferences("values", Context.MODE_PRIVATE);
    editor = preferences.edit();

    setButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            setButton.setEnabled(false);
            setButton.setText("Done");
            editor.putBoolean("appSet", true).apply();

            handlerDelay = 10000;
            editor.putLong("handlerDelay", handlerDelay).apply();

            editor.putString("handlerType", "Notification").apply();

            Intent intentService = new Intent(activityContext, mainActivity.service.class);
            startService(intentService);

            lockRunnable = new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(activityContext, "Lock Handler", Toast.LENGTH_SHORT).show();
                }
            };

            handler = new Handler();
            notificationRunnable = new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(activityContext, "Notification Handler", Toast.LENGTH_SHORT).show();

                    editor.putString("handlerType", "lock").apply();
                    editor.putLong("handlerDelay", 10 * 1000).apply();
                    handlerDelay = preferences.getLong("handlerDelay", 0);
                    handler.postDelayed(lockRunnable, handlerDelay);
                }
            };
            handler.postDelayed(notificationRunnable, handlerDelay);
        }
    });
}

static public class screenStateReceiver extends BroadcastReceiver {

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

        preferences = context.getSharedPreferences("values", Context.MODE_PRIVATE);
        editor = preferences.edit();

        handler = new Handler();

        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {

            handler.removeCallbacksAndMessages(null);
        }

        if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {

            handlerDelay = preferences.getLong("handlerDelay", 0);
            editor.putLong("handlerDelay", handlerDelay).apply();

            handlerType = preferences.getString("handlerType", "notification");

            if(!(handlerDelay < 0)) {
                if (handlerType.equals("notification")) {
                    notificationRunnable = new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(context, "Notification Handler", Toast.LENGTH_SHORT).show();

                            editor.putString("handlerType", "lock").apply();
                            editor.putLong("handlerDelay", 10 * 1000).apply();
                            handlerDelay = preferences.getLong("handlerDelay", 0);
                            handler.postDelayed(lockRunnable, handlerDelay);
                        }
                    };
                    handler.postDelayed(notificationRunnable, handlerDelay);
                }

                else if(handlerType.equals("lock")) {
                    lockRunnable = new Runnable() {
                        @Override
                        public void run() {
                            if(handlerType.equals("lock")) {
                                Toast.makeText(context, "Lock Handler", Toast.LENGTH_SHORT).show();
                                Log.e("lock handler", "Lock Handler");
                                editor.putString("handlerType",null).apply();
                            }
                        }
                    };
                    handler.postDelayed(lockRunnable, handlerDelay);
                }
            }
        }
    }
}

static public class service extends Service {

    BroadcastReceiver mReceiver;

    @Override
    public void onCreate() {
        super.onCreate();
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        mReceiver = new screenStateReceiver();
        registerReceiver(mReceiver, filter);
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        unregisterReceiver(mReceiver);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
}

应用应该在启动第二个可运行对象后启动另一个可运行对象。

这是因为在 BroadcastRecevier 中您再次实例化处理程序。您应该删除 handler = new Handler();,否则您将在另一个 Handler

上调用 removeCallbacks