现在的位置: 首页 > 综合 > 正文

如何在代码中重启手机

2013年02月12日 ⁄ 综合 ⁄ 共 1525字 ⁄ 字号 评论关闭

已解决:

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

需在Android.mk中添加:

LOCAL_CERTIFICATE := platform

包含Android.mk的工程须在Linux下编译才有效。

 

----------------------------------------------------------------------------

 

引用 6 楼 kinghua_q 的回复:

要不你自己做了个JNI接口试试,在C语言里面直接system("reboot");

如果权限不出问题的话,应该是可以的

试了这种方法,应该是权限不够,其他命令都可以,如system("mkdir /sdcard/test")没问题,reboot不行,su后reboot也不行,也不知道怎么获取权限。

 

---------------------------------------------------------------------------

看问题要看本质,任何系统级应用都会到linux的系统调用中去, 对于reboot来讲,会调用到int reboot (int mode) 或者直接用 __reboot来做, 所以你只要change mode的值就ok, mode 里有
#define RB_AUTOBOOT LINUX_REBOOT_CMD_RESTART
#define RB_HALT_SYSTEM LINUX_REBOOT_CMD_HALT
#define RB_ENABLE_CAD LINUX_REBOOT_CMD_CAD_ON
#define RB_DISABLE_CAD LINUX_REBOOT_CMD_CAD_OFF
#define RB_POWER_OFF LINUX_REBOOT_CMD_POWER_OFF

这些可以选择,power off , autoboot 对应的就是开关机了。 当然您需要root或者system的权限,或者SHELL好像也没问题。

----------------------------------------------------------------------------

 

To shichexixi ,你好,我直接采用的是用源码编译的,你可以根据提供的文件去查看源代码中GOOGLE是怎么调用关机重启的,更深入的理解就要参考wiki14 帖子中提到的代码了。注意添加权限。

关机:
In frameworks/base/services/java/com/android/server/BatteryService.java

Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);

  intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);

  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

  mContext.startActivity(intent);

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

谢谢各位的热心解答。

 

抱歉!评论已关闭.