设备管理器使用

Android 2.2 SDK提供了一个可管理和操作设备的API叫DevicePolicyManager,使用这个API你可以接管手机的应用权限,对手机做出很多大胆的操作,比如锁屏、恢复出厂设置、设置密码、强制清除密码,修改密码、设置屏幕灯光渐暗时间间隔等操作。

这个API让你的程序直接掌握了系统的命脉。

DeviceAdminReceiver的具体实现类DeviceAdmin ,它是继承自Receiver 广播接收者;

package com.example.lock;

import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;

/** * DeviceAdminReceiver的具体实现类它是继承自Receiver 广播接收者; * @author Administrator * */
public class DeviceAdmin extends DeviceAdminReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        super.onReceive(context, intent);
    }
}
package com.example.lock;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

@SuppressLint("ShowToast")
public class MainActivity extends Activity implements OnClickListener {
    private Button bt_re_pw, bt_lock, bt_other, bt_clean;
    private ComponentName mDeviceAdminSample;// 组件对象;
    private DevicePolicyManager mDPM;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUI();
        mDeviceAdminSample = new ComponentName(this, DeviceAdmin.class);// 广播接收者对应的字节码文件;
        mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);

    }

    private void initUI() {
        bt_clean = (Button) findViewById(R.id.bt_clean);
        bt_re_pw = (Button) findViewById(R.id.bt_re_pw);
        bt_lock = (Button) findViewById(R.id.bt_lock);
        bt_other = (Button) findViewById(R.id.bt_other);
        bt_clean.setOnClickListener(this);
        bt_re_pw.setOnClickListener(this);
        bt_lock.setOnClickListener(this);
        bt_other.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.bt_clean:// 在真机上请慎用;
            if (mDPM.isAdminActive(mDeviceAdminSample)) {// 设备管理器是否激活
                // mDPM.wipeData(0);//WIPE_EXTERNAL_STORAGE清楚sd卡的数据;
                // mDPM.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);//WIPE_EXTERNAL_STORAGE清楚sd卡的数据;
            } else {
                Toast.makeText(this, "请先激活", Toast.LENGTH_SHORT).show();
            }
            break;
        case R.id.bt_re_pw://卸载应用程序
            Intent intent = new Intent("android.intent.action.DELETE");
            intent.addCategory("android.intent.category.DEFAULT");
            intent.setData(Uri.parse("package:" + getPackageName()));//根据唯一的包名进行卸载;
            startActivity(intent);
            break;
        case R.id.bt_lock:
            if (mDPM.isAdminActive(mDeviceAdminSample)) {// 设备管理器是否激活

                mDPM.lockNow();
                // 锁屏的同时激活密码;
                mDPM.resetPassword("123", 0);
            } else {
                Toast.makeText(this, "请先激活", Toast.LENGTH_SHORT).show();
            }

            break;
        case R.id.bt_other:
            Intent intent2 = new Intent(
                    DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent2.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
                    mDeviceAdminSample);
            intent2.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "设备管理器");
            startActivity(intent2);
            break;

        default:
            break;
        }

    }

}

在res目录下创建一个文件夹命名为xml在其中创建一个xml文件device_admin_sample.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>

以下是配置清单文件;

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

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

    <application  android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" >
        <activity  android:name=".MainActivity" android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <receiver  android:name="com.example.lock.DeviceAdmin" 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>

其中的命名就是随便写的请大神见谅。

相关文章
相关标签/搜索