一键锁屏、一键删除、设备管理器

设备管理器的操作步骤

1、创建类DeviceAdminReceiver的子类

package com.itheima62.lockscreen;

import android.app.admin.DeviceAdminReceiver;

public class DeviceAdminSample extends DeviceAdminReceiver {

}

2,在清单文件中配置广播接收者

<receiver
            android:name="com.itheima62.lockscreen.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>

 

3,在strings.xml文件中配置字符串相关信息

<string name="activity_sample_device_admin">设备管理员</string>
<string name="sample_device_admin">管理员</string>
<string name="sample_device_admin_description">简介</string>

4,在res目录下创建xml文件夹,在该文件夹下创建device_admin_sample.xml文件,内容:

<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
  <uses-policies>
    <limit-password />
    <watch-login />
    <reset-password />
    <force-lock />
    <wipe-data />
    <expire-password />
    <encrypted-storage />
    <disable-camera />
  </uses-policies>
</device-admin>

5,在代码中创建设备管理器和组件

DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);

//看使用场景如果不需要这个可以不写
ComponentName who = new ComponentName(this, DeviceAdminSample.class);

6,写功能

dpm.lockNow();//一键锁屏

dmp.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);//清除数据
 

 

源码实例

DeviceAdminSample.java

package com.itheima62.lockscreen;

import android.app.admin.DeviceAdminReceiver;

public class DeviceAdminSample extends DeviceAdminReceiver {

}
View Code

 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.itheima62.lockscreen"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.itheima62.lockscreen.MainActivity"
            android:label="一键锁屏" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <activity
            android:name="com.itheima62.lockscreen.RemoveActivity"
            android:label="一键卸载" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       
        
        

        <receiver
            android:name="com.itheima62.lockscreen.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>
    </application>

</manifest>
View Code

 

String.xml

<resources>

    <string name="app_name">一键锁屏</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="activity_sample_device_admin">设备管理员</string>
    <string name="sample_device_admin">管理员</string>
    <string name="sample_device_admin_description">开启设备管理员,不开启扣2000块</string>
</resources>
View Code

 

device_admin_sample.xml

<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
  <uses-policies>
    <limit-password />
    <watch-login />
    <reset-password />
    <force-lock />
    <wipe-data />
    <expire-password />
    <encrypted-storage />
    <disable-camera />
  </uses-policies>
</device-admin>
View Code

 

MainActivity.java

package com.itheima62.lockscreen;

import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

    private DevicePolicyManager dpm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
        lockScreen(null);
    }
    
    /**
     * @param v
     * 一键锁屏
     */
    public void lockScreen(View v){
        
        //如果没有激活设备管理员,提醒给用户做事
        ComponentName who = new ComponentName(this, DeviceAdminSample.class);
        if (dpm.isAdminActive(who)) {//true
            dpm.lockNow();//一键锁屏
            finish();
        } else {
            
              Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
              intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, who);
              intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                     "设备管理器,,,,,,,,,,,,,,,,");
              startActivityForResult(intent, 1);
            
        }
        
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
View Code

 

RemoveActivity.java

package com.itheima62.lockscreen;

import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class RemoveActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        remove(null);
        
    }
     
    /**
     * 一键卸载
     * @param v
     */
    public void remove(View v){
        //调用卸载的界面
        /* <intent-filter>
         <action android:name="android.intent.action.VIEW" />
         <action android:name="android.intent.action.DELETE" />
         <category android:name="android.intent.category.DEFAULT" />
         <data android:scheme="package" />
     </intent-filter>*/
        
        // 取消激活设备管理
        DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
        ComponentName who = new ComponentName(this, DeviceAdminSample.class);
        dpm.removeActiveAdmin(who);//取消激活管理设备
        
        //卸载
        Intent remove = new Intent("android.intent.action.DELETE");
        remove.addCategory("android.intent.category.DEFAULT");
        remove.setData(Uri.parse("package:" + getPackageName()));
        startActivity(remove);//卸载用户apk的界面
    }
}
View Code

 

posted @ 2016-03-15 12:51  znyyjk  阅读(749)  评论(0编辑  收藏  举报