Reputation: 14091
How to know Android Phone is going to sleep? Please Help me with a sample code. Thanks for reading.
Upvotes: 1
Views: 1294
Reputation: 142
You'll need to register a broadcastreceiver for Intent.ACTION_SCREEN_OFF
So, create the broadcastreceiver
This is where you handle the screen_off intent.
private BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(final Context context, final Intent intent) {
/*
* dispatch screen_off
* to handler method
*/
String iAction = intent.getAction();
if (iAction.equals(Intent.ACTION_SCREEN_OFF))
handleScreenAction(iAction);
}
};
Now the filter to register the receiver.
static void registerReciever() {
IntentFilter myFilter = new IntentFilter();
// Catch screen off event
myFilter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(receiver, myFilter);
}
Upvotes: 1