最近研究了一下android关机跟重新启动功能。网上的文章也不少,做法也有一些。笔者试过了几种方法,下面介绍一下:
<一>、 Android重启功能
在androidjava层执行shell命令来完成。但是笔者在开发测试中同样的代码发现用Eng编译出的版本可以重新启动,user版本不能完成重启,挂在关机那个界面。
/*****************************************************************************************************/
声明:本博内容均由http://blog.csdn.net/sundesheng125原创,转载请注明出处,谢谢!
/*****************************************************************************************************/
用shell命令,eng版本能完成重新启动的代码如下:
[java] view plaincopyprint?
String cmd = "su -c reboot";
exeShell(cmd);
public void exeShell(String cmd){
try{
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader in = new BufferedReader(
new InputStreamReader(
p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
Log.i("exeShell",line);
}
}
catch(Throwable t)
{
t.printStackTrace();
}
}
String cmd = "su -c reboot";
exeShell(cmd);
public void exeShell(String cmd){
try{
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader in = new BufferedReader(
new InputStreamReader(
p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
Log.i("exeShell",line);
}
}
catch(Throwable t)
{
t.printStackTrace();
}
}
提示的错误是权限问题,但是代码是一样的,manifest里面也给了REBOOT权限,在网上找了一下解释还是没有什么好的解决方案,错误信息如下:
[plain] view plaincopyprint?
01-01 08:05:26.319 W/System.err( 776): java.io.IOException: Error running exec(). Command: [su, -c, reboot] Working Directory: null Environment: null
01-01 08:05:26.319 W/System.err( 776): at java.lang.ProcessManager.exec(ProcessManager.java:211)
01-01 08:05:26.319 W/System.err( 776): at java.lang.Runtime.exec(Runtime.java:168)
01-01 08:05:26.319 W/System.err( 776): at java.lang.Runtime.exec(Runtime.java:241)
01-01 08:05:26.319 W/System.err( 776): at java.lang.Runtime.exec(Runtime.java:184)
01-01 08:05:26.319 W/System.err( 776): at com.android.settings.DevelopmentSettings.exeShell(DevelopmentSettings.java:591)
01-01 08:05:26.319 W/System.err( 776): at com.android.settings.DevelopmentSettings.onPreferenceChange(DevelopmentSettings.java:580)
01-01 08:05:26.319 W/System.err( 776): at android.preference.Preference.callChangeListener(Preference.java:885)
01-01 08:05:26.319 W/System.err( 776): at android.preference.ListPreference.onDialogClosed(ListPreference.java:265)
01-01 08:05:26.329 W/System.err( 776): at android.preference.DialogPreference.onDismiss(DialogPreference.java:381)
01-01 08:05:26.329 W/System.err( 776): at android.app.Dialog$ListenersHandler.handleMessage(Dialog.java:1235)
01-01 08:05:26.329 W/System.err( 776): at android.os.Handler.dispatchMessage(Handler.java:99)
01-01 08:05:26.329 W/System.err( 776): at android.os.Looper.loop(Looper.java:137)
01-01 08:05:26.329 W/System.err( 776): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-01 08:05:26.329 W/System.err( 776): at java.lang.reflect.Method.invokeNative(Native Method)
01-01 08:05:26.329 W/System.err( 776): at java.lang.reflect.Method.invoke(Method.java:511)
01-01 08:05:26.329 W/System.err( 776): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-01 08:05:26.329 W/System.err( 776): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-01 08:05:26.329 W/System.err( 776): at dalvik.system.NativeStart.main(Native Method)
01-01 08:05:26.329 W/System.err( 776): Caused by: java.io.IOException: Permission denied
01-01 08:05:26.329 W/System.err( 776): at java.lang.ProcessManager.exec(Native Method)
01-01 08:05:26.339 W/System.err( 776): at java.lang.ProcessManager.exec(ProcessManager.java:209)
01-01 08:05:26.339 W/System.err( 776): ... 17 more
01-01 08:05:26.369 W/InputManagerService( 174): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@4138ecb8
01-01 08:05:26.319 W/System.err( 776): java.io.IOException: Error running exec(). Command: [su, -c, reboot] Working Directory: null Environment: null
原文转自:http://blog.csdn.net/sundesheng125/article/details/8494810