Alexander Hermann
Alexander Hermann

Reputation: 13

how to get if phone come back from sleep mode Android

It would be possible to receive if phone is come back from sleep mode? I need a receiver. OnScreenOn or interactive is to check if the screen is On or Off. But I need to get exactly when the screen come back from sleep mode. Android. API 23 and up.

Upvotes: 0

Views: 739

Answers (1)

Alberto S.
Alberto S.

Reputation: 7649

I don't know if I understood you correctly but it seems like you need a BroadcastReceiver

You have more info in this answer

For the sake of completeness I'll add it here:

Add the following to your manifest:

<receiver android:name=".UserPresentBroadcastReceiver">
  <intent-filter>
    <action android:name="android.intent.action.USER_PRESENT" />
    <action android:name="android.intent.action.ACTION_SHUTDOWN" />
 </intent-filter>
</receiver>

Handle the actions:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class UserPresentBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent intent) {

        /*Sent when the user is present after 
         * device wakes up (e.g when the keyguard is gone)
         * */
        if(intent.getAction().equals(Intent.ACTION_USER_PRESENT)){

        }
        /*Device is shutting down. This is broadcast when the device 
         * is being shut down (completely turned off, not sleeping)
         * */
        else if (intent.getAction().equals(Intent.ACTION_SHUTDOWN)) {

        }
    }

}

Upvotes: 1

Related Questions