自己写android的关机命令

<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"
#endif

int 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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值