top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Android 4.4.2 : How to turn off screen automatically

+3 votes
369 views

After using SCREEN_ON SCREEN_OFF broadcast receiver, screen does not seem to switch off automatically as it used to for default android app.

None of the code commented below works for Android 4.4.2 (API 19 onward)

int time = android.provider.Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, Integer.MAX_VALUE);
// Toast.makeText(context, "Screen Time out is "+new Date(lastTimeScreenOn+15000) + ", lastTimeScreenOn = "+new Date(lastTimeScreenOn), Toast.LENGTH_SHORT).show();
// screen off if more than 15 sec
if(System.currentTimeMillis() - lastTimeScreenOn > 15000)
 {
      Toast.makeText(context, "Screen Time out ", Toast.LENGTH_LONG).show();
      //DevicePolicyManager mDPM = (DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE);
      //mDPM.lockNow();
      //Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 1000);
 }
posted Jul 8, 2016 by Priya Ranjan Kumar

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

I have come across a similar question in this link.

It seems you should first receive admin access for the device. Then, you have to call lockNow method of DevicePolicyManager instance.

Note: I haven't tested the code my self. Please let me know if this doesn't work. I will look into what's wrong.

answer Jul 13, 2016 by Vinod Kumar K V
Similar Questions
0 votes

I understand not allowing apps to automatically install home screen widgets as it could be annoying but for n00bish users it can be a challenge to instruct them on how to do so and so it DRASTICALLY weakens the business case for building a widget (and thereby diminishes a defining feature of Android).
To remedy this I wanted to suggest either an API that allows for installing a home screen widget after a confirmation with the user and/or an app permission that allows an app to install home screen widgets and/or a feature on Google Play that could take care of the confirming and installing.

+2 votes

I want to test bluetooth on Android Development environment any suggestions?

+2 votes

I am trying to figure out the best way to get a device to only use LTE and not GSM. I want to know would it be better to write an up to attempt to force the android to stay in LTE only or in the android OS and make the change.

...