Android关机重启实现
Android通常是在长按Power键后出现弹出菜单,然后可以选择Power Off进行关机操作。代码实现在
frameworks/policies/base/phone/com/android/internal/policy/impl/PhoneWindowManager.java(mPowerLongPress
Runnable)
frameworks/policies/base/phone/com/android/internal/policy/impl/GlobalActions.java
下面是个示例程序实现关机重启和恢复出厂设置,在KK上测试通过
1. Android.mk
LOCAL_PACKAGE_NAME := PowerOff
LOCAL_CERTIFICATE := platform
2. AndroidManifest.xml
package="com.***.poweroff"
android:sharedUserId="android.uid.system"
<uses-permission android:name="android.permission.SHUTDOWN"/>
<uses-permission android:name="android.permission.MASTER_CLEAR" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity
android:name=".PowerOff"
android:label="@string/app_name" >
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
3. PowerOff.java
ok_poweroff.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Log.d(TAG, "Power off starting");
Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
intent.putExtra(Intent.EXTRA_KEY_CONFIRM, true);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
ok_restart.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Log.d(TAG, "Restarting");
Intent intent = new Intent(Intent.ACTION_REBOOT);
intent.putExtra("nowait", 1);
intent.putExtra("interval", 1);
intent.putExtra("window", 0);
sendBroadcast(intent);
}
});
ok_factoryreset.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Log.d(TAG, "Factory reset starting");
sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
}
});