APK中实现关机

本文介绍了如何在APK中实现关机功能,包括两种实现方式:将代码放入自定义源码中编译,或者在Eclipse中加载frameworks的classes.jar。关键在于加载顺序需置于Android.jar之前,并在APK中添加android:sharedUserId="android.uid.system"属性,且需用platform签名。此外,解释了为何直接通过Intent.ACTION_SHUTDOWN无法实现关机的原因。
摘要由CSDN通过智能技术生成
不像重启可以使用PowerManager.reboot(),Android 对于关机没有开放的API可以使用,因此只能使用源码中的方法,比如系统没电了总会关机吧

代码位置:

BatteryService.java (amdroid_march\android\frameworks\base\services\java\com\android\server)

private final void shutdownIfNoPower() {
        // shut down gracefully if our battery is critically low and we are not powered.
        // wait until the system has booted before attempting to display the shutdown dialog.
        if (mBatteryLevel == 0 && !isPowered() && ActivityManagerNative.isSystemReady()) {
            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);
        }
    }

不过APK在eclipse中编译不了,解决方法有两种

一种方法是放到自己的源码中编译

一种方法是eclipse加载frameworks的classes.jar,

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值