/** * 激活设备管理器 * * @param context * 上下文对象 */ public static void activeApp(Context context) { if (context instanceof Activity) { activeApplication(context); } else { Intent in = new Intent(context, StartDeviceManagerActivity.class); in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(in); } }
/** * 激活设备管理器 * * @param context * 上下文对象 */ private static void activeApplication(Context context) { DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); ComponentName cm = new ComponentName(context, MyDeviceAdminReceiver.class); if (!dpm.isAdminActive(cm)) { Intent in = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); in.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, cm); in.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, context.getPackageName()); context.startActivity(in); } }
StartDeviceManagerActivity:
package com.home.util; import android.app.Activity; import android.os.Bundle; public class StartDeviceManagerActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ApplicationUtil.activeApp(this); finish(); } }
配置:
<!-- 激活需要传入的广播 --> <receiver android:name="com.home.util.MyDeviceAdminReceiver" android:permission="android.permission.BIND_DEVICE_ADMIN" > <meta-data android:name="android.app.device_admin" android:resource="@xml/device_admin" /> <intent-filter> <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" /> </intent-filter> </receiver> <!-- 启动激活的页面 --> <activity android:name="com.home.util.StartDeviceManagerActivity" android:theme="@style/TransparentActivity" />
device_admin.xml:
<device-admin xmlns:android="http://schemas.android.com/apk/res/android"> <uses-policies> <wipe-data /> </uses-policies> </device-admin>
原文地址:http://blog.csdn.net/u010142437/article/details/41356779