想做一个可以通过程序控制直接关机的功能,想适应所有设备的,打算用反射来做,但是发现无论是ShutdownActivity还是ShutdownThread 都需要系统权限:
这是来自开源社区的回答:http://www.oschina.net/question/781675_75563
在此感谢回答问题的那位同仁,不过这样关机时,系统直接黑掉了,如果能像系统的关机画面那样自然就好了,路过的朋友也可以说说你们使用过的好方法,thanks!
android.permission.REBOOT,而且ShutdownThread 这个对象怎么也射不进去,因为取不到这个对象,是android内部的货,有人说要编译源码才能做到。于是决定先把root的设备适应了,找了一大堆的参考,发现都差不多,但是都没解决我的问题,后来终于找到一个经过测试靠谱的:
import java.io.DataOutputStream;
import java.io.IOException;
final class Rebooter extends Thread
{
public static final String CMDREBOOT = "reboot";
public static final String CMDSHUTDOWN = "reboot -p";
private final String strEnter = "\n";
private final String cmd_su = "su";
private final String cmd_exit = "exit";
private String cmd = "";
public Rebooter()
{
}
public void reboot(String command)
{
if (!command.equals(CMD_REBOOT) && !command.equals(CMD_SHUTDOWN))
return;
cmd = command;
start();
}
@Override
public void run()
{
try
{
sleep(1000);
Process localProcess = Runtime.getRuntime().exec(cmd_su);
DataOutputStream localDataOutputStream = new DataOutputStream(
localProcess.getOutputStream());
localDataOutputStream.writeBytes(cmd + strEnter);
localDataOutputStream.writeBytes(cmd_exit + strEnter);
localDataOutputStream.flush();
localDataOutputStream.close();
localProcess.waitFor();
localProcess.destroy();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
这是来自开源社区的回答:http://www.oschina.net/question/781675_75563
在此感谢回答问题的那位同仁,不过这样关机时,系统直接黑掉了,如果能像系统的关机画面那样自然就好了,路过的朋友也可以说说你们使用过的好方法,thanks!