admin管理员组

文章数量:1122853

android恢复出厂设置,关机,重启以及系统升级和充电器连接广播

恢复出厂设置

<uses-permission android:name="android.permission.MASTER_CLEAR" />
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
} else {Intent intent = new Intent("android.intent.action.FACTORY_RESET");intent.setPackage("android");sendBroadcast(intent);
}

关机

<uses-permission android:name="android.permission.SHUTDOWN" />
Intent intent = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
intent.putExtra("android.intent.extra.KEY_CONFIRM", true);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent,0);

重启

<uses-permission android:name="android.permission.REBOOT" />
Intent intent = new Intent("android.intent.action.REBOOT");
intent.putExtra("nowait",1);
intent.putExtra("interval",1);
intent.putExtra("window",0);
sendBroadcast(intent);

系统升级

<uses-permission android:name="android.permission.ACCESS_CACHE_FILESYSTEM" />
<uses-permission android:name="android.permission.REBOOT" />
File packageFile = new File("/sdcard/update.zip"); 
RecoverySystem.installPackage(getApplicationContext(), packageFile);
升级最后会调用系统的frameworks\base\core\jave\android\os\RecoverySystem.java中的installPackage函数,
可以通过adb shell进入到tmp目录,然后cat recovery.log来查看升级log注:Android4.4以前把APK放到/system/app目录才有权限; 
Android4.4以后设置android:sharedUserId="android.uid.system" 后使用系统签名
或:
在/cache/recovery/command文件中写入--update_package=/cache/update.zip
然后adb reboot recovery也可以实现升级

充电器连接广播

public class PowerStateReceiver extends BroadcastReceiver {private static Context context = null;private static PowerToken powerToken = null;@Overridepublic void onReceive(Context mContext, Intent intent) {context = mContext;if(powerToken==null)powerToken = new PowerToken();if (intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED)) {Toast.makeText(mContext, "ACTION_POWER_DISCONNECTED", Toast.LENGTH_LONG).show();powerToken.setPowerConnected(false);new Thread(new Runnable() {@Overridepublic void run() {synchronized (powerToken) {try {powerToken.wait(3000);} catch (Exception e) {Debug.printf(e.toString(), null);}}if(IsPowerConnected()==false) {//TODO ACC 断开三秒关机}}}).start();}else if(intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {Toast.makeText(mContext, "ACTION_POWER_CONNECTED", Toast.LENGTH_LONG).show();powerToken.setPowerConnected(true);synchronized (powerToken) {powerToken.notifyAll();}}}public static boolean IsPowerConnected() {if(powerToken==null) return true;return powerToken.isPowerConnected();}private class PowerToken {private boolean powerconnected = true;public void setPowerConnected(boolean connected) {this.powerconnected = connected;}public boolean isPowerConnected() {return (powerconnected == true);}}private void shutdown() {Intent shutdownIntent = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");shutdownIntent.putExtra("android.intent.extra.KEY_CONFIRM", false);shutdownIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);try {//防止APK不在System目录时,无权限导致崩溃context.startActivity(shutdownIntent);} catch (Exception e) {Debug.printf(e.toString(), null);}}
}
<!-- 充电器连接广播 -->
<receiver android:name="PowerStateReceiver" ><intent-filter><action android:name="android.intent.action.ACTION_POWER_CONNECTED" /><action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" /></intent-filter>
</receiver>

本文标签: Android恢复出厂设置关机重启以及系统升级和充电器连接广播