关闭设备编程 [英] Turn off device programmatically

查看:218
本文介绍了关闭设备编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的目的是在一个特定的设备型号(即晶晨半导体运行基于固件的一个Android的机顶盒设备)上运行的应用程序。我有两个根本的能力和我的应用程序与固件的证书签名。

I am writing an App that is designed to run on one specific device model (an Android set-top device that runs Amlogic based firmware). I have both root capability and my App is signed with the firmware certificate.

我的应用程序是该装置的主要焦点,这将是能够发起一个完整断电很有帮助。

My App is the main focus of the device, and it would be helpful to be able to initiate a complete power-off.

我没有关闭命令。我确实有重启命令。

I do not have the shutdown command. I do have the reboot command.

重启-p 于事无补。它只是冻结装置而其余开机。

reboot -p does not help. It simply freezes the device while remaining powered on.

电源管理器是一步到位,但它设置,而不是完全关闭设备进入睡眠模式,:

The PowerManager is one step better, but it sets the device into sleep mode, instead of a complete shutdown:

PowerManager pm = (PowerManager)getSystemService(Service.POWER_SERVICE);
pm.goToSleep(SystemClock.uptimeMillis());

我向所有人开放的建议 - 哈克或以其他方式。安卓的版本预计将维持在 4.2.2

意图

此命令将导致设备重启。 Intent.ACTION_SHUTDOWN 似乎并没有做任何事情。这是意图也许只有到报告关机,而不是启动一个?

This command will cause the device to reboot. Intent.ACTION_SHUTDOWN does not appear to do anything. Is this Intent perhaps only to report a shutdown, and not to initiate one?

Intent i = new Intent(Intent.ACTION_REBOOT);
i.putExtra("nowait", 1);
i.putExtra("interval", 1);
i.putExtra("window", 0);
sendBroadcast(i);

最幸运我与这要求关闭受意图​​:

The most luck I had with this was to request a shutdown by Intent:

Intent i = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
i.putExtra("android.intent.extra.KEY_CONFIRM", true);
startActivity(i);


关闭线程

这是一个有点接近。绝对有趣。你能找到使用它的例子吗?

That is a bit closer. Definitely interesting. Can you find an example of using it?

到目前为止,我想出了这一点:

So far I have come up with this:

Class<?> sdClass = Class.forName("com.android.server.power.ShutdownThread");
Constructor<?> con = sdClass.getDeclaredConstructors()[0];
con.setAccessible(true);

for (Method m : sdClass.getDeclaredMethods()) {
    if (m.getName().matches("shutdown")) {
        m.setAccessible(true);
        m.invoke(sdClass, PlayerActivity.this, false);
    } else if (m.getName().matches("rebootOrShutdown")) {
        m.setAccessible(true);
        m.invoke(sdClass, PlayerActivity.this, false);
    } else if (m.getName().matches("beginShutdownSequence")) {
        m.setAccessible(true);
        m.invoke(sdClass, PlayerActivity.this, false);
    }
}

关闭 beginShutdownSequence 创建 NullPointerException异常 S(做你知道为什么吗?)和 rebootOrShutdown 创建一个的InvocationTargetException 由于一个的UnsatisfiedLinkError ...它不能找到一个本地方法:

shutdown and beginShutdownSequence create NullPointerExceptions (do you see why?) and rebootOrShutdown creates an InvocationTargetException due to an UnsatisfiedLinkError... It cannot find a native method:

java.lang.UnsatisfiedLinkError中:本机方法未找到:   com.android.server.power.PowerManagerService.nativeShutdown :()V在   com.android.server.power.PowerManagerService.nativeShutdown(母语   法)以   com.android.server.power.PowerManagerService.lowLevelShutdown(PowerManagerService.java:2163)     在   com.android.server.power.ShutdownThread.rebootOrShutdown(ShutdownThread.java:543)     在   com.android.server.power.ShutdownThread.run(ShutdownThread.java:393)

java.lang.UnsatisfiedLinkError: Native method not found: com.android.server.power.PowerManagerService.nativeShutdown:()V at com.android.server.power.PowerManagerService.nativeShutdown(Native Method) at com.android.server.power.PowerManagerService.lowLevelShutdown(PowerManagerService.java:2163) at com.android.server.power.ShutdownThread.rebootOrShutdown(ShutdownThread.java:543) at com.android.server.power.ShutdownThread.run(ShutdownThread.java:393)

lowLevelShutdown 是所有的功能,最终达到,当设置为关闭(而不是重新启动)功能。因此,搞清楚如何避免此链接错误可能是关键。

lowLevelShutdown is the function that all the functions eventually reach, when configured to shutdown (and not reboot). So figuring out how to avoid this link error may be key.

推荐答案

在我而言,我不认为这是可能的关闭设备我怎么想。

In my case, I do not think it is possible to shut the device down how I would like to.

这是我所管理的最接近的是使用去我的目标:

The closest that I managed to get to my target was using:

Intent i = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
i.putExtra("android.intent.extra.KEY_CONFIRM", true);
startActivity(i);

这带来了一个对话框,关闭设备。这是一个完美的解决方案,但对我来说,使用它会导致设备崩溃。这可能是我的设备是有点特殊,和其他装置将没有这些限制。

That brings up a dialog to turn the device off. This is the perfect solution, but in my case, using it causes the device to crash. It may be that my device is somewhat special, and other devices will not have these restrictions.

在任何情况下,我希望我的测试将帮助其他人在他们的追求。

In any case, I hope that my testing will help others in their quest.

这篇关于关闭设备编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆