- 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权限,在网上找了一下解释还是没有什么好的解决方案,错误信息如下:
- 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 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
另外网上还有一种做法,代码如下:
- Intent i = new Intent(Intent.ACTION_REBOOT);
- i.putExtra("nowait", 1);
- i.putExtra("interval", 1);
- i.putExtra("window", 0);
- sendBroadcast(i);
Intent i = new Intent(Intent.ACTION_REBOOT); i.putExtra("nowait", 1); i.putExtra("interval", 1); i.putExtra("window", 0); sendBroadcast(i);
不过这种要做签名,比较麻烦,笔者没有试过。
笔者后面又在framework下面找了一下相关功能得代码,在
./frameworks/base/tests/DumpRenderTree2/src/com/android/dumprendertree2/ManagerService.java文件中有一个新的方法,模仿这个方法,笔者完成了既能在eng版本,也能在user版本中完成重新启动的程序。具体代码如下:
- Intent iReboot = new Intent(Intent.ACTION_REBOOT);
- iReboot.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- mContext.startActivity(iReboot);
Intent iReboot = new Intent(Intent.ACTION_REBOOT); iReboot.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); mContext.startActivity(iReboot);
<二>、关机功能:
关机功能相对来说更容易些,应该不够android的手机还是平板,长按电源都会弹出一个关机的对话框,模仿一下相应的代码就可以实现,在framework下面也有一个shutdown的一块处理模块。关机的具体代码如下:
- Intent shutdown = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
- shutdown.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
- shutdown.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- mContext.startActivity(shutdown);
Intent shutdown = new Intent(Intent.ACTION_REQUEST_SHUTDOWN); shutdown.putExtra(Intent.EXTRA_KEY_CONFIRM, false); shutdown.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); mContext.startActivity(shutdown);
<三>、关于context
笔者的关机或重启功能是在:【setting】--》【开发人员选项】里面增加了一项【关闭车载平板电脑】,在DevelopmentSettings.java中,这里面不是普通的activitie,是一个PreferenceFragment,所以要得到context,需要使用如下方法:
mContext = getActivity();
不能使用getContext();view有getContext的方法,fragment有getActivity的方法,activity就是context的子类,context不一定是activity,但是activity一定是context,总之,程序一定要找到上下文,否则intent什么的,都不可以操作。
一些功能得开发,很多能在android源码里找到类似功能得代码,在framework下面有一下test代码,也很有用,搜索一下总能找到一些解决办法。