使用Android设备中的设备策略管理器恢复出厂设置

时间:2017-11-07 12:47:16

标签: android

我想使用设备策略管理器点击按钮进行恢复出厂设置。 那可能吗?如果是,我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

最后我解决了我的问题,      在这里我把我的代码。

首先添加您的清单文件,

<receiver
        android:name=".MainActivity$DeviceAdminSample"
        android:description="@string/sample_device_admin_description"
        android:label="@string/sample_device_admin"
        android:permission="android.permission.BIND_DEVICE_ADMIN">
        <meta-data
            android:name="android.app.device_admin"
            android:resource="@xml/device_admin_sample" />
        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
        </intent-filter>
    </receiver>

而不是你的activity_main.xml,

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mantra.brightnessdemo.MainActivity">

<Button
    android:id="@+id/btn_factory"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

  </android.support.constraint.ConstraintLayout>

添加Mainactivity.java,

public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE_ENABLE_ADMIN = 1;
Button btn_factory;
DevicePolicyManager mDPM;
ComponentName mDeviceAdmin;
int currentAPIVersion;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn_factory = (Button) findViewById(R.id.btn_factory);
    currentAPIVersion = Build.VERSION.SDK_INT;

    if (currentAPIVersion >= android.os.Build.VERSION_CODES.FROYO) {
        //2.2+
        mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        mDeviceAdmin = new ComponentName(this, DeviceAdminSample.class);
    }
    btn_factory.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (currentAPIVersion >= android.os.Build.VERSION_CODES.FROYO) {
                // 2.2+
                if (!mDPM.isAdminActive(mDeviceAdmin)) {
                    Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdmin);
                    intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Process will remove user installed applications, settings, wallpaper and sound settings. Are you sure you want to wipe device?");
                    startActivityForResult(intent, REQUEST_CODE_ENABLE_ADMIN);
                } else {
                    // device administrator, can do security operations
                    mDPM.wipeData(0);
                }

            } else {
                // 2.1
                try {
                    Context foreignContext = createPackageContext("com.android.settings", Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
                    Class<?> yourClass = foreignContext.getClassLoader().loadClass("com.android.settings.MasterClear");
                    Intent i = new Intent(foreignContext, yourClass);
                    startActivityForResult(i, REQUEST_CODE_ENABLE_ADMIN);
                } catch (ClassNotFoundException e) {

                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }

            }
        }
    });


}

public class DeviceAdminSample extends DeviceAdminReceiver {

    void showToast(Context context, String msg) {
        String status = context.getString(R.string.admin_receiver_status, msg);
        Toast.makeText(context, status, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onEnabled(Context context, Intent intent) {
        showToast(context, context.getString(R.string.admin_receiver_status_enabled));
    }

    @Override
    public CharSequence onDisableRequested(Context context, Intent intent) {
        return context.getString(R.string.admin_receiver_status_disable_warning);
    }

    @Override
    public void onDisabled(Context context, Intent intent) {
        showToast(context, context.getString(R.string.admin_receiver_status_disabled));
    }

    @Override
    public void onPasswordChanged(Context context, Intent intent) {
        showToast(context, context.getString(R.string.admin_receiver_status_pw_changed));
    }

}

}

这对我有用。