以编程方式关闭 Android 手机

发布于 2024-09-24 12:15:25 字数 353 浏览 4 评论 0原文

我们可以通过编程方式关闭 Android 手机吗?

我正在使用以下代码片段,但它对我不起作用。

KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE); 
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE); 

lock.disableKeyguard(); // to disable

lock.reenableKeygaurd();// to enable

我也使用了该许可。

Can we switch off an Android phone programmatically?

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

KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE); 
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE); 

lock.disableKeyguard(); // to disable

lock.reenableKeygaurd();// to enable

and I used the permission also.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

黑白记忆 2024-10-01 12:15:25

正如 CommonsWare 已经说过的,这在普通 SDK 应用程序中是不可能的。您需要使用系统固件密钥对您的应用进行签名。但您的应用程序可以拥有 Root 权限。尝试使用以下代码(如果您有 SU 访问权限)

关机:

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

重新启动:

相同的代码,只需使用“rebo​​ot”< /code> 而不是“rebo​​ot -p”


[另一方面:我在某处读到这些命令不适用于 Stock HTC ROM,但尚未证实]

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]

简美 2024-10-01 12:15:25

您无法通过普通的 SDK 应用程序执行此操作。只有使用系统固件签名密钥签名的应用程序才能执行此操作。

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

海的爱人是光 2024-10-01 12:15:25

您可以使用 PowerManager 使其重新启动(这并不能保证它会重新启动 - 操作系统可能会取消它):

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

它需要 REBOOT 权限:

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

您是否可以在尝试启用/禁用键盘保护时检查您的 logcat,并发布其中的内容?

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?

拥抱没勇气 2024-10-01 12:15:25

更好的解决方案是运行:

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

您可以为此使用 Process

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.

胡大本事 2024-10-01 12:15:25

如果您拥有 sigOrSystem 权限 android.permission.SHUTDOWN,您可以像这样引发受保护的 ACTION_REQUEST_SHUTDOWN Intent:

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);

请注意,在 Android 4.0 之前,此权限仅是签名权限。

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.

青柠芒果 2024-10-01 12:15:25

事实上,根据我的经验,上述回答并不完全准确。我正在尝试调暗屏幕的方法,发现以下内容:

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

如果我使用屏幕亮度值 0,而不是 0.005,实际上会关闭我的 Samsung Galaxy Tab。

我怀疑这是某个地方的错误,但我不这么认为有足够的硬件在其他 Android 型号上测试代码。因此,我无法真正告诉您手机上会发生什么。我可以告诉你,即使完全未签名,我的代码也会关闭我的手机。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文