Programmatically switching off Android phone

后端 未结 6 1613
清歌不尽
清歌不尽 2020-11-22 13:02

Can we switch off an Android phone programmatically?

I am using following snippet but it didn\'t work for me.

KeyguardManager keyguardManager = (Keyg         


        
相关标签:
6条回答
  • 2020-11-22 13:40

    Actually, the above responses are not completely accurate, in my experience. I was experimenting with ways to dim the screen and found that the following:

    Window w = getWindow();
    WindowManager.LayoutParams lp = w.getAttributes();
    lp.screenBrightness =.005f;
    w.setAttributes (lp);
    

    will actually turn my Samsung Galaxy Tab off if, instead of 0.005, I use a screen brightness value of 0.

    I suspect this is bug somewhere, but I don't have sufficient hardware to test the code on other Android models. Hence, I can't really tell you what will happen on your phone. I can tell you that my code shuts of my phone even completely unsigned.

    0 讨论(0)
  • 2020-11-22 13:41

    You could possibly use the PowerManager to make it reboot (this does not guarantee that it'll reboot - OS may cancel it):

    http://developer.android.com/reference/android/os/PowerManager.html#reboot(java.lang.String)

    It requires the REBOOT permission:

    http://developer.android.com/reference/android/Manifest.permission.html#REBOOT

    Can you also check your logcat when trying to enable/disable keyguard, and post what's there?

    0 讨论(0)
  • 2020-11-22 13:41

    If you have the sigOrSystem permission android.permission.SHUTDOWN you can raise the protected ACTION_REQUEST_SHUTDOWN Intent like this:

    Intent intent = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
    intent.putExtra("android.intent.extra.KEY_CONFIRM", false);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    

    Note that prior to Android 4.0, this permission was signature only.

    0 讨论(0)
  • 2020-11-22 13:44

    A much better solution is to run:

    su -c am start -a android.intent.action.ACTION_REQUEST_SHUTDOWN

    You can use a Process for this.

    0 讨论(0)
  • 2020-11-22 13:45

    You cannot do this from an ordinary SDK application. Only applications signed with the system firmware signing key can do this.

    0 讨论(0)
  • 2020-11-22 13:58

    As CommonsWare already said that this is not possible in an Ordinary SDK Application. You need to sign your app with the System Firmware Key. But it's possible for your app with Root privileges. Try using the following code (if you have SU access):

    Shutdown:

    try {
        Process proc = Runtime.getRuntime()
                        .exec(new String[]{ "su", "-c", "reboot -p" });
        proc.waitFor();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    

    Restart:

    Same code, just use "reboot" instead of "reboot -p".


    [On an other note: I read somewhere that these commands do not work on Stock HTC ROMs, but haven't confirmed myself]

    0 讨论(0)
提交回复
热议问题