简体   繁体   English

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

[英]Factory Reset with Device policy manager in android device

I want to do factory reset on click of button using Device Policy Manager. 我想使用“设备策略管理器”单击按钮以恢复出厂设置。 Is that possible? 那可能吗? if yes than how can i do that? 如果是,那我该怎么办?

Finally i solved my question, here I put my code. 终于我解决了我的问题,在这里放了我的代码。

first add in your manifest file, 首先添加清单文件,

<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>

than in your activity_main.xml, 比您的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>

add in you Mainactivity.java, 在您添加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));
    }

}

}

this is works for me. 这对我有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 设备策略管理器 - 重置密码 - Android 3.0问题 - Device Policy Manager - Reset Password - Android 3.0 Problems 无法使用设备策略管理器在Android 7中重置密码 - Can't Reset Password in Android 7 with Device Policy Manager 是否可以为 Android 编写不可移动的设备策略管理器? - Is it possible to write an unremovable device policy manager for Android? 设备策略管理器无法在Android上设置重置密码令牌(引发“当前用户禁用了托管令牌”异常) - Device policy manager is unable to set reset password token on Android (“Escrow token is disabled on the current user” exception is thrown) 有没有办法以编程方式在 Android 设备上执行恢复出厂设置? - Is there a way to programmatically perform a factory reset on an Android device? Android设备管理API-恢复出厂设置,IMEI跟踪 - Android Device Administration API - factory reset, IMEI tracking android:恢复出厂设置后,设备管理应用是否会被删除? - android: Does device admin apps get deleted after factory reset? Sqlite数据库是否可在设备擦除/出厂重置时永久保存为android系统应用程序 - Is Sqlite database persistent for android system apps on Device Wipe / Factory Reset 如何获取Android设备的上次恢复出厂设置时间? - How to obtain the last factory reset time for an Android device? Android设备管理器未显示设备管理器 - Android Device Manager not showing the device manager
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM