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