Solved

How to turn on screen programmatically in Android?

Posted on 2014-09-26
1
1,807 Views
Last Modified: 2014-10-31
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?
0
Comment
Question by:miyahira
1 Comment
 
LVL 17

Accepted Solution

by:
Chris Harte earned 500 total points
ID: 40354439
It looks like that FLAG_SHOW_WHEN_LOCKED has been set to not work with password locked devices.

https://code.google.com/p/android-developer-preview/issues/detail?id=1150
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The case of the missing phone talks about the way a small electronic gadget (the mobile phone) has penetrated into our lives and has made us addicted to it.
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now