0

i can detect when the screen turns off in this way:

IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_ON);
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            Toast.makeText(MainActivity.this, "Screen off", Toast.LENGTH_SHORT).show();
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            Toast.makeText(MainActivity.this, "Screen on", Toast.LENGTH_SHORT).show();
        }
    }
}, intentFilter);

I need do the same thing, but the Toast will have to appears after a certain seconds that i can set through a timePicker. So every time the screen turns off, if i set 10 seconds, after 10 seconds i will get a toast. How can i do it? Do i need a service? Manually i can do something like this:

new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {

             // here the code
        }
}, 30 * 1000);

But if i could set the seconds with a time picker will be better. Thanks

Atlas91
  • 5,754
  • 17
  • 69
  • 141

1 Answers1

0

Doing a timer is fine, but a Toast on a powered off screen... you understand it will never appear, right?

If you want to apply a delay, prefer Executors.newSingleThreadScheduledExecutor() over Handler.postDelayed().

Should definitely work. Now you want the user to input the delay.

Just use a TimePicker dialog, and set the delay in its callback.

Community
  • 1
  • 1
shkschneider
  • 17,833
  • 13
  • 59
  • 112
  • Ok assuming use a log, so i can see it in the logcat. The example set the timer using the code.. like my example.. What exactly i need it's the possibility to set the timer with a picker. I mean, the user can set the timer manually. Right? – Atlas91 Apr 24 '14 at 12:46