3

My app has a service that registers ScreenStateReceiver which creates a handler when the screen is on and removes it when the screen is off.

My problem is that handler.removeCallbacksAndMessages doesn't work when the screen is off. I know that because I get multiple logs from all the handlers created whenever the screen is on.

My code:

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;
    }
}
}

The app is supposed to start another runnable after starting the second runnable.

Gurfuffle
  • 784
  • 12
  • 32
idk
  • 57
  • 1
  • 5

1 Answers1

3

it is because in the BroadcastRecevier you are instantiating the handler again. You should remove handler = new Handler();, otherwise you are calling removeCallbacks on a different Handler

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • The receiver should work even when the app is turned off. I think erasing that line would make handler null? – idk May 17 '15 at 17:50
  • it could. Still this doesn't change the reason why removeCallbacks is not working in your case – Blackbelt May 17 '15 at 17:52
  • Ok, thanks for the help. I initialized `handler` in the service's `onCreate` method and now it works well. – idk May 17 '15 at 19:04