tags:

views:

61

answers:

1

How to know Android Phone is going to sleep? Please Help me with a sample code. Thanks for reading.

A: 

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);
}
Android Dev Dude
Nothing is happening Can you provide the full code please It will be a great help
Jim