Android恢复出厂设置

0.应用必须是系统应用

也就是apk需要放置在/system/priv-app/xxx/aaa.apk , 或/system/app/ 中

可以不声明android.uid.system

1.权限声明

    
    
    
    
    

2.写入recovery命令并重启系统到recovery模式自动执行恢复出厂设置

 private void reset(){


        boolean writeCommand = true;


        File dir = new File("/cache/recovery/");
        if (!dir.exists()){
            dir.mkdirs();
        }
        File command = new File(dir,"command");
        if (command.exists()){
            command.delete();
        }


        File log = new File(dir,"log");
        if (log.exists()){
            log.delete();
        }


        StringBuffer args = new StringBuffer();
        args.append("--wipe_data");
        args.append("\n");
        args.append("--wipe_cache");
        args.append("\n");
        FileWriter commandF = null;
        try {
            commandF = new FileWriter(command);
            commandF.write(args.toString());
        } catch (IOException e) {
            e.printStackTrace();
            writeCommand = false;
        }finally {
            if (commandF != null){
                try {
                    commandF.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


        if (!command.exists()){
            writeCommand = false;
        }


        if (writeCommand){
            PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
            pm.reboot("recovery");
        }else{
            Toast.makeText(MainActivity.this,"写入恢复命令失败",Toast.LENGTH_LONG).show();
        }



    }

 

 

原理

通过powerManager 重启到recovery , 系统在进入recovery模式时会去 /cache/recovery/command  文件中读取指令,根据指令参数进行下一步操作。深入原理不再赘述。

参考

Android 恢复出厂设置分析 :https://www.jianshu.com/p/cc099f5b6e73

Android Recovery具体工作原理 :https://blog.csdn.net/wu070815/article/details/8199046

Android Recovery 源码解析和界面定制 :https://blog.csdn.net/qq_33575901/article/details/81129801

Android Recovery升级原理:https://blog.csdn.net/luzhenrong45/article/details/60968458

 

你可能感兴趣的:(android,技巧)