关于恢复出厂设置的说明

原创 2016年06月01日 15:52:23

恢复出厂设置分为两种 ,要么清除sdcard么不清除。无论哪种都是通过发送广播的形式来执行这个操作。

private void doMasterClear() {
    if (mEraseSdCard) {
        Intent intent = new Intent(ExternalStorageFormatter.FORMAT_AND_FACTORY_RESET);
        intent.putExtra(Intent.EXTRA_REASON, "MasterClearConfirm");
        intent.setComponent(ExternalStorageFormatter.COMPONENT_NAME);
        getActivity().startService(intent);
    } else {
        Intent intent = new Intent(Intent.ACTION_MASTER_CLEAR);
        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
        intent.putExtra(Intent.EXTRA_REASON, "MasterClearConfirm");
        getActivity().sendBroadcast(intent);
        // Intent handling is asynchronous -- assume it will happen soon.
    }

}

其中接收广播并执行操作的是com.android.server.MasterClearReceiver这个类。

public class MasterClearReceiver extends BroadcastReceiver {
private static final String TAG = “MasterClear”;

@Override
public void onReceive(final Context context, final Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_REMOTE_INTENT)) {
        if (!"google.com".equals(intent.getStringExtra("from"))) {
            Slog.w(TAG, "Ignoring master clear request -- not from trusted server.");
            return;
        }
    }

    final boolean shutdown = intent.getBooleanExtra("shutdown", false);
    final String reason = intent.getStringExtra(Intent.EXTRA_REASON);

    Slog.w(TAG, "!!! FACTORY RESET !!!");
    // The reboot call is blocking, so we need to do it on another thread.
    Thread thr = new Thread("Reboot") {
        @Override
        public void run() {
            try {
                RecoverySystem.rebootWipeUserData(context, shutdown, reason);
                Log.wtf(TAG, "Still running after master clear?!");
            } catch (IOException e) {
                Slog.e(TAG, "Can't perform master clear/factory reset", e);
            } catch (SecurityException e) {
                Slog.e(TAG, "Can't perform master clear/factory reset", e);
            }
        }
    };
    thr.start();
}

}

其中 RecoverySystem.rebootWipeUserData(context, shutdown, reason);做了实际清除userdata等等操作
中rebootWipeUserData 代码如下。

/**
 * Reboots the device and wipes the user data and cache
 * partitions.  This is sometimes called a "factory reset", which
 * is something of a misnomer because the system partition is not
 * restored to its factory state.  Requires the
 * {@link android.Manifest.permission#REBOOT} permission.
 *
 * @param context   the Context to use
 * @param shutdown  if true, the device will be powered down after
 *                  the wipe completes, rather than being rebooted
 *                  back to the regular system.
 *
 * @throws IOException  if writing the recovery command file
 * fails, or if the reboot itself fails.
 * @throws SecurityException if the current user is not allowed to wipe data.
 *
 * @hide
 */
public static void rebootWipeUserData(Context context, boolean shutdown, String reason)
        throws IOException {
    UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
    if (um.hasUserRestriction(UserManager.DISALLOW_FACTORY_RESET)) {
        throw new SecurityException("Wiping data is not allowed for this user.");
    }
    final ConditionVariable condition = new ConditionVariable();

    Intent intent = new Intent("android.intent.action.MASTER_CLEAR_NOTIFICATION");
    intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
    context.sendOrderedBroadcastAsUser(intent, UserHandle.OWNER,
            android.Manifest.permission.MASTER_CLEAR,
            new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    condition.open();
                }
            }, null, 0, null, null);

    // Block until the ordered broadcast has completed.
    condition.block();

    String shutdownArg = null;
    if (shutdown) {
        shutdownArg = "--shutdown_after";
    }

    String reasonArg = null;
    if (!TextUtils.isEmpty(reason)) {
        reasonArg = "--reason=" + sanitizeArg(reason);
    }

    final String localeArg = "--locale=" + Locale.getDefault().toString();
    bootCommand(context, shutdownArg, "--wipe_data", reasonArg, localeArg);

}

其中:
/**
* Reboot into the recovery system with the supplied argument.
* @param args to pass to the recovery utility.
* @throws IOException if something goes wrong.
*/
private static void bootCommand(Context context, String… args) throws IOException {
RECOVERY_DIR.mkdirs(); // In case we need it
COMMAND_FILE.delete(); // In case it’s not writable
LOG_FILE.delete();

    FileWriter command = new FileWriter(COMMAND_FILE);
    try {
        for (String arg : args) {
            if (!TextUtils.isEmpty(arg)) {
                command.write(arg);
                command.write("\n");
            }
        }
    } finally {
        command.close();
    }

    // Having written the command file, go ahead and reboot
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    pm.reboot(PowerManager.REBOOT_RECOVERY);

    throw new IOException("Reboot failed (no permissions?)");
}

关机,然后开机,底层判断节点后进入恢复出厂模式,recevory.img释放完全后,进入开机的流程。。。至于具体想知道系统做了什么可以查看源代码找一下后续逻辑和行为。
我这仅仅是发送广播。

Android6.0 Reset恢复出厂设置流程分析

点击Settings应用中的恢复出厂设置按钮后流程分析: 先使用grep命令搜索"恢复出厂设置"字符串,找到相应的布局文件: packages/apps/Settings/res/xml/priv...

android N 恢复出厂设置流程简析

前言: 恢复出厂设置的本质上是先写入command到BCB中,然后让手机进入recovery模式,此时会根据BCB中的命令来执行对应的操作,本文主要分析java层的流程。力有不逮,如有错漏,请多指...
  • Bing_ka
  • Bing_ka
  • 2017年07月05日 18:14
  • 536

android高通平台调用恢复出厂设置的方法

百度上解析恢复出厂设置的过程有很多的资料,一搜一堆,这里我只说在高通平台上如何调用恢复出厂设置的方法。  进入package/app/Settings下find -name MasterClea...

android 恢复出厂设置流程

在MasterClearConfirm.java中显示恢复出厂提示和对应button,点击button后调用button的 click方法  1. 如果选中erase sd card, 则star...

Android恢复出厂设置

恢复出厂设置核心代码:sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));即发送一个广播,需要在AndroidManifes...
  • brian512
  • brian512
  • 2014年11月07日 22:23
  • 20540

恢复出厂设置

/Settings/src/com/android/settings/MasterClearConfirm.java     private void doMasterClear() {     ...

用 devenv.exe /resetuserdata 恢复Visual Studio 初始状态

/resetuserdata 参数如果 Visual Studio 在运行时被损坏,且无法从损坏状态进行恢复,您可以使用此参数将 Visual Studio 重置到其使用之初的状态。这些问题的例子可能...
  • kgdnui
  • kgdnui
  • 2009年08月20日 11:10
  • 3485

Android Back&reset(备份与恢复出产设置)

一、恢复出产设置主要流程:在settings–>Backup & reset—>恢复出厂设置—>恢复手机或者平板出厂设置—>清除全部内容—>设备关机—>开机—>进行恢复出厂的操作—>开机流程附上一张时...

系统重置

这个设置在android源码下的代码如下: if (mEraseSdCard) { Intent intent = new Intent(ExternalStorageFormatter.F...

Android--Recovery模块之恢复出厂设置

主要讲解一下Recovery模块的恢复出厂设置流程,上层入口主要是将恢复出厂设置命令写入/cache/recovery/command文件,并进行重启;重启后系统进入bootable的recovery...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:关于恢复出厂设置的说明
举报原因:
原因补充:

(最多只允许输入30个字)