How to make android device always in wake mode?

2020-05-31 05:29发布

问题:

After successful root of device. Now, I need to make device always in wake state i.e. always visibility of UI and no black screen or any daydream screens. To do so I think I've to accomplish following :

  1. No lock screen - turned off
  2. Sleep set to “never”
  3. Daydream set to “off”

What I found is all about application layer i.e. there are some applications which can do above tasks. But since my mobile is rooted I want to achieve with system files so that even if some other applications try to change above functionalities then they should not be able to do so.

回答1:

  1. Activate developer mode
  2. go to Developer options
  3. turn on "Stay awake"


回答2:

Your main use case appears to be as follows (from your question)

Even if some other applications try to change above functionalities then they should not be able to do so

You can write a system service to trigger the PowerManager.WakeLock at regular intervals. (Source)

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
wl.acquire();

// screen and CPU will stay awake during this section

wl.release();

To optimize the service you can also try setting the screen timeout the the maximum possible again at regular intervals so that even if changed manually it gets reset. (not sure how much max is allowed, you'll need to check with trial and error)

  /**
   * set screen off timeout
   * @param screenOffTimeout int 0~6
   */
private void setTimeout(int screenOffTimeout) {
    int time;
    switch (screenOffTimeout) {
    case 0:
        time = 15000;
        break;
    case 1:
        time = 30000;
        break;
    case 2:
        time = 60000;
        break;
    case 3:
        time = 120000;
        break;
    case 4:
        time = 600000;
        break;
    case 5:
        time = 1800000;
        break;
    default:
        time = -1;
    }
    android.provider.Settings.System.putInt(getContentResolver(),
            Settings.System.SCREEN_OFF_TIMEOUT, time);
}

(Source)



回答3:

I think you will need to modify the framework...Build framework.jar and push it to the device.

You can refer this link to know how to modify framework code-Modify java code in framework.jar and https://devmaze.wordpress.com/2011/01/18/using-com-android-internal-part-2-hacking-around/

Other alternative I can think of is using some MDM(Mobile device management) solution from Airwatch/SOTI/SAP Afaria which have privilege APIs to control the device.



回答4:

Ok So after reading your question carefully, I found that you are not looking for any programmatic solution. You don't want to install an app to do that what you want is to modify the system file to achieve this functionality automatically.

Basically you want to modify the file who is exposing its methods to other apps by PowerManager class to control the sleep time and wake lock. Two things you need to know here:

  1. The system file you are looking for is available in jar format not in text or any other form.
  2. If somehow you will locate that file and start playing with that then you would end up breaking the compiled version of Android framework and your phone may misbehave after that.

Conclusion I understand you want to do something tricky by doing this but unfortunately it can't be done this way and even if you do so by playing with that file then all operations of your phone handled by PowerManager class will start playing with you. :)



回答5:

To prevent the sleep mode on a specific View, just call setKeepScreenOn(true) on that View or set the keepScreenOn property to true.

In turns it will prevent the screen from going off while the View is on the screen. It works without requiring the WAKE_LOCK permission (or any special permission) , which is a better practice here.

Also this will not force the phone to stay awake outside the life span of the application. You can run into that problem with WakeLock

You can integrate the above method with another approach to get the full solution (outside an application layer):

From the root shell (e.g. adb shell), you can lock with:

echo mylockname >/sys/power/wake_lock 

After which the device will stay awake, until you do:

echo mylockname >/sys/power/wake_unlock    

With the same string for 'mylockname'.

Note that this will not prevent the screen from going black, but it will prevent the CPU from sleeping.

Note that /sys/power/wake_lock is read-write for user radio (1001) and group system (1000), and, of course, root.

Source: https://lwn.net/Articles/479841/

Source: https://stackoverflow.com/a/18968753/3125120



回答6:

If you want to increase screen timeout via adb shell command follow below steps:

Steps

  1. Turn on USB debugging
  2. Connect android device through usb cable
  3. Change directory to android-sdk/platform-tools/
  4. In terminal check device is connected by using below command: $ adb devices
  5. Issue screen timeout command: $ adb shell settings put system screen_off_timeout 60000

Note

60000 = 1 minute



回答7:

So from Developer Options the best you could get is 30 minutes or so of "Wake" ...

According to this, there are a few good applications that can override the functionality and keep your screen on indefinitely:

  1. Keep Screen On
  2. Stay Alive
  3. Wakey

UPDATE

You will need to do research into how Android's file system is structured and how it works, then go and modify its core files. There is no easy way to do what you require. The only way is to create an Android App, that will never have an onDestroy(), and will continuously run in the background. You can use a WakeLock to achieve that.

Look here for how to use the WakeLock