Android turn on the screen

巧了我就是萌 提交于 2019-12-10 19:30:48

问题


I making the application where an activity launch is scheduled by AlarmManager. I would like to appear even if the screen is turned off and device is locked.

To achive this a set the Window flags

final Window win = getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
        | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
        | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
        | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);

And try to obtain lock for the screen

if(_lock == null)
{
    PowerManager pm = (PowerManager)App.getAppContext()
            .getSystemService(Context.POWER_SERVICE);

    _lock = pm.newWakeLock(
            PowerManager.FULL_WAKE_LOCK, "ScreenOn");
    _lock.acquire();
}

The _lock is PowerManager.WakeLock which is released in onPause

protected void onPause()
{
     if(_lock != null)
     {
          _lock.release();
     }
}

This code is executed in onCreate and onRestart. Everything works OK if the activity is not launched yet.

But if it was launched earlier the screen is not turned off.

  • onRestart is called first
  • onResume is then called
  • onPause is called immediately

So the activity is not launched. My question is how to turn on the screen in such situation. (I am using API 15).


回答1:


I came up with the solution. I created a new activity which will be trying to turn on the screen in the onCreate() and then wait until it is turned on. When the screen is ok it will launch the activity which should be displayed. To make the Android always create this activity

public class TurnOnScreen extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        if (pm.isScreenOn()) openActivity();
        else {
            registerReceiver(mScreenOnReceiver, new IntentFilter(
                    Intent.ACTION_SCREEN_ON));
            reciever_registered = true;
            turnScreenOn();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (reciever_registered) {
            unregisterReceiver(mScreenOnReceiver);
            reciever_registered = false;
        }
    }

    private boolean reciever_registered = false;
    private final BroadcastReceiver mScreenOnReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            openActivity();
        }
    };

    private void openActivity() {
        Intent intent = new Intent();
        // ....
        finish();
    }

    private void turnScreenOn() {
        final Window win = getWindow();
        win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
    }
}

I am still looking for explanations why the screen is not turned on in onRestart.




回答2:


Have you heard of "The Lighted Green Room"? Check out the code below, it may be what you're looking for.

http://code.google.com/p/ch-bfh-fbi-mobicomp-2011/source/browse/ch_simplix_android_repetitive_service/src/com/androidbook/longrun/LightedGreenRoom.java?spec=svn38&r=37




回答3:


Just use your code :

final Window win = getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
        | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

in onCreate() only and remove all those other Activity-Cycle methods if they are not doing anything else then this.

I don't think you need any more code to use to perform it.



来源:https://stackoverflow.com/questions/16344667/android-turn-on-the-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!