Link to home
Start Free TrialLog in
Avatar of miyahira
miyahiraFlag for Peru

asked on

How to turn on screen programmatically in Android?

I have developed an App that loads automatically when smartphone receives an SMS message.

Problem is that when screen's phone is off or in "black" mode, App doesn't load when SMS arrives. I need that my app turns screen on programmatically. Is that possible? I found several past solutions but no one has worked.

I tried:
WindowManager.LayoutParams params =
                getWindow().getAttributes();
                params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
                params.screenBrightness = 0;
                getWindow().setAttributes(params);

Open in new window

And tried:
super.onCreate(savedInstanceState);
setContentView(R.layout.my_main_activity);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

Open in new window

And no success.

I've been advised that this code could do the trick:

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

Open in new window


However, Android Studio shows me error:
Cannot resolve symbol 'FLAG_SHOW_WHEN_LOCKED'

Open in new window



Is there any import or setting that I should do?
ASKER CERTIFIED SOLUTION
Avatar of Chris Harte
Chris Harte
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial