自己写android的关机命令
来源:互联网 发布:关于网络利弊 编辑:程序博客网 时间:2024/05/24 05:40
<span style="font-size: 16px;">pad</span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">中自带有重启命令(</span><span style="font-size: 16px;">reboot</span></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">),本以为也应该有关机命令啊,可是找来找去也没有诸如</span><span style="font-size: 16px;">shutdown</span></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">、</span><span style="font-size: 16px;">poweroff</span></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">等命令。然后想到</span><span style="font-size: 16px;">busybox</span></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">中有一个</span><span style="font-size: 16px;">poweroff</span></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">命令,故又去编译了一个</span><span style="font-size: 16px;">busybox</span></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">推进去,执行了</span><span style="font-size: 16px;">busybox poweroff</span></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">之后看了下返回码是</span><span style="font-size: 16px;">0</span></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">(即</span><span style="font-size: 16px;">echo $?</span></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">),然后,然后什么也没发生……</span></span>
<span style="background-image: none; background-attachment: scroll; background-position: 0% 0%;"><span style="font-size: 16px;">网上流行的方法是在</span><span style="font-size: 16px;">Java</span></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%;"><span style="font-size: 16px;">端发送要求关机的</span><span style="font-size: 16px;">Intent</span></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%;"><span style="font-size: 16px;">,如下:</span></span>
<span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;"></span></span><pre name="code" class="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);
<span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">需要加上相关的权限:</span></span><span style="font-size: 16px;">android:sharedUserId="android.uid.system"</span><span style="font-size: 16px;"><uses-permission android:name="android.permission.SHUTDOWN"/></span><span style="font-size: 16px;"></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">但要从</span><span style="font-size: 16px;">apk</span></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">中实现关机的话,那还不如手动关呢,</span></span><span style="font-size: 16px;"><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"></span></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">所以自己动手了,写个诸如</span><span style="font-size: 16px;">shutdown</span></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">的小命令,实现也很简单,找到系统怎么处理关机的,然后我们把那段代码拿出来就行了。</span></span><span style="font-size: 16px;"></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">系统中处理关机部分的代码路径:</span><span style="font-size: 16px;">frameworks/base/core/jni/android_os_Power.cpp</span></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">,里面有个</span><span style="font-size: 16px;">android_os_Power_shutdown</span></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">方法,具体如下:</span></span><pre name="code" class="java">static void android_os_Power_shutdown(JNIEnv *env, jobject clazz){ sync();#ifdef HAVE_ANDROID_OS reboot(RB_POWER_OFF);#endif}
<span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">看到有个</span><span style="font-size: 16px;">reboot</span></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">函数,然后再追踪这个</span><span style="font-size: 16px;">reboot(RB_POWER_OFF)</span></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">函数,看到其最终会调用</span><span style="font-size: 16px;">__reboot</span></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">这个函数(汇编实现的),我们只要给它传递“合适”的参数就行了。</span></span><span style="font-size: 16px;"> </span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">代码实现:</span></span><span style="font-size: 16px;">1</span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">、在</span><span style="font-size: 16px;">Android</span></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">源码目录中的</span><span style="font-size: 16px;">packages/apps/</span></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">下创建一个目录,例如:</span><span style="font-size: 16px;">Hello</span></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">。</span></span><span style="font-size: 16px;">2</span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">、编写</span><span style="font-size: 16px;">Android.mk</span></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">文件:</span></span><span style="font-size: 16px;">LOCAL_PATH:= $(call my-dir)</span><span style="font-size: 16px;">include $(CLEAR_VARS)</span><span style="font-size: 16px;"><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"></span></span><span style="font-size: 16px;">LOCAL_SRC_FILES:= shutdown.c</span><span style="font-size: 16px;"><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"></span></span><span style="font-size: 16px;">LOCAL_MODULE:= shutdown </span><span style="font-size: 16px;">LOCAL_PRELINK_MODULE:= false</span><span style="font-size: 16px;">LOCAL_SHARED_LIBRARIES:= libutils</span><span style="font-size: 16px;">include $(BUILD_EXECUTABLE)</span><span style="font-size: 16px;">3</span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">、编写</span><span style="font-size: 16px;">shutdown.c</span></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">源文件,如下:</span></span>
#include <stdio.h>#include <stdlib.h>#include <utils/Log.h>#include <sys/reboot.h>#ifndef LOG_TAG#define LOG_TAG "dxyh"#endifint main(int argc, char **argv){int retval;if ((retval = __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,RB_POWER_OFF, NULL)) < 0) {LOGE("Try to shutdown the machine failed!");exit(EXIT_FAILURE);}return 0;}
<span style="font-size: 16px;">4</span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">、</span><span style="font-size: 16px;">mm</span></span><span style="font-size: 16px;">5</span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">、将生成的</span><span style="font-size: 16px;">shutdown</span></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">可执行程序推入到</span><span style="font-size: 16px;">pad</span></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">中的</span><span style="font-size: 16px;">/system/bin</span></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">下即可。</span></span><span style="font-size: 16px;">6</span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">、然后就可以和其他命令一样运行了,例如</span><span style="font-size: 16px;">adb shell shutdown</span></span><span style="background-image: none; background-attachment: scroll; background-color: transparent; background-position: 0% 0%; background-repeat: repeat repeat;"><span style="font-size: 16px;">。</span></span>
0 0
- 自己写android的关机命令
- 自己实现Android关机命令
- 自己实现Android关机命令
- 自己实现Android关机命令
- 自己写的关机、注销、重启小程序
- 写自己的命令
- 写自己的ls命令
- 写自己的ls命令
- android-----关机命令---------------
- 如何在自己的板子上实现android关机
- Windows 的关机命令
- 树莓派的关机命令
- 关机的dos命令
- 如何来写自己的ls命令
- 如何来写自己的ls命令
- 自己写Linux下的pwd命令
- 如何来写自己的ls命令
- Commander写自己的Nodejs命令
- Android模拟器上不了网的解决办法
- PHPCMS V9调用栏目
- IOS 代码实现自动布局(视图尺寸自动调整)
- 算法————笔试内容--->二叉搜索树
- HDOJ 1251 统计难题 字典树
- 自己写android的关机命令
- Qt UDP的初步使用
- Unity3D游戏游戏化之DrawCall优化过程分析详解
- java获得CPU使用率,系统内存,虚拟机内存等情况
- LINQ初步
- HDOJ 1880 魔咒词典
- AFNetworking下载文件断点续传操作
- 多个子线程并发执行,主线程等待所有子线程都结束后再继续运行
- java List set()方法用法