سؤال

I found post from two year ago that somebody said that it can't be done. I also found that I can use ACTION_USER_PRESENT to detect when a user unlock the screen, but can I detect when the user turns on the screen (by any action)?

EDIT: I want to know when the user press any button or do anything else that can turn on the screen

هل كانت مفيدة؟

المحلول 2

I started writing this as a comment but it turned out too big.

I couldn't find anything either. You can't have a key listener on services and you apparently can't register a broadcast receiver for the power button, only for media buttons. Maybe you can do this on the broadcast receiver for the screen on and check which is the current activity)? Like this, for example:

ActivityManager am = (ActivityManager) this .getSystemService(ACTIVITY_SERVICE);
List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
Log.d(WebServiceHelper.TAG, "CURRENT Activity ::" + taskInfo.get(0).topActivity.getClassName()+"   Package Name :  "+ componentInfo.getPackageName());

If you can get detect that the activity with the focus is the lockscreen, you increase the chances of being the user who turned on his/her phone. Maybe an alternative method on the broadcast receiver is to detect if the phone is locked but I guess this will be usually the case no matter the current activity being displayed:

KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
boolean locked = km.inKeyguardRestrictedInputMode();

Hope this can help somehow. It's an interesting question.

نصائح أخرى

The system will broadcast Intent.ACTION_SCREEN_ON which you can use to catch the event when the screen turns on.

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            // your code here

        }
    }
}

Note that you can't broadcast this event yourself.

Are you looking for this?

@Override
public void onConfigurationChanged (Configuration newConfig)

and put this in your manifest for each activity that you want orientation notification:

android:configChanges="orientation" >
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top