开发者

How to know Android Phone is going to sleep?

开发者 https://www.devze.com 2023-01-16 05:36 出处:网络
How to know Android Phone is goin开发者_StackOverflow中文版g to sleep? Please Help me with a sample code.

How to know Android Phone is goin开发者_StackOverflow中文版g to sleep? Please Help me with a sample code. Thanks for reading.


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

精彩评论

暂无评论...
验证码 换一张
取 消