关闭

添加当前程序到设备管理列表里

标签: android
320人阅读 评论(0) 收藏 举报
分类:

1、权限配置

详细请参考Device Administration以及DeviceAdminReceiver的相关文档
根据sdk文档,说明,先创建Androidmanifest.xml,它必须包含如下信息

  1. 子类(继承自DeviceAdminReceiver)需要包含以下两点:

    • 必须拥有BIND_DEVICE_ADMIN
    • 能处理ACTION_DEVICE_ADMIN_ENABLED的intent请求,并在xml中声明为一个intent
  2. 在metadata中进行安全声明

    下面是一个manifest例子

        <receiver
            android:name=".myDeviceAdminReceiver"
            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>
a) meta-data项是必须的,还有权限以及intent-filter
b) android:resource对应的xml需要自己创建,并放在xml目录下,xml目录没有自己创建,其内容如下:
<?xml version="1.0" encoding="utf-8"?>
<device-admin
  xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-policies>
        <force-lock />
    </uses-policies>
</device-admin>

2、代码部分,创建继承子类MyDeviceAdminReceiver

public class myDeviceAdminReceiver extends DeviceAdminReceiver{

    private static String TAG = "TestHello"; 
    @Override
    public void onEnabled(Context context, Intent intent) {
        // TODO Auto-generated method stub
        super.onEnabled(context, intent);
        Toast.makeText(context, context.getResources().getString(R.string.device_admin_jihuo_success_tips), Toast.LENGTH_LONG).show();;
        Log.i(TAG, "DeviceAdminReceiver::onEnabled");
    }

    @Override
    public CharSequence onDisableRequested(Context context, Intent intent) {
        // TODO Auto-generated method stub
        //这里如果返回的字符串不为空,那么当用户去设置里取消时,则会提示带此文字的确定框
        String strResult = "请不要取消,谢谢!!!";
        Log.i(TAG, "DeviceAdminReceiver::onDisableRequested");
        //return super.onDisableRequested(context, intent);
        return strResult;
    }

    @Override
    public void onDisabled(Context context, Intent intent) {
        // TODO Auto-generated method stub
        super.onDisabled(context, intent);
        Log.i(TAG, "DeviceAdminReceiver::onDisable");
        Toast.makeText(context, context.getResources().getString(R.string.device_admin_cancel_jihuo_tips), Toast.LENGTH_LONG).show();;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        super.onReceive(context, intent);
        Log.i(TAG, "DeviceAdminReceiver::onReceive");
    }

}

3、编写添加进入设备的代码

    public void OpenDevicePolicyManager(Context context)
    {
        DevicePolicyManager dManager = (DevicePolicyManager) context.getSystemService("device_policy");//获取manager对象
        ComponentName localComponentName = new ComponentName(this, myDeviceAdminReceiver.class);
        if (dManager != null && 
            localComponentName != null && 
            false == dManager.isAdminActive(localComponentName)) {
            Intent localIntent = new Intent(dManager.ACTION_ADD_DEVICE_ADMIN);
            localIntent.putExtra(dManager.EXTRA_DEVICE_ADMIN, localComponentName);
            localIntent.putExtra(dManager.EXTRA_ADD_EXPLANATION, 
                                 context.getResources().getString(R.string.device_admin_explanation));
            startActivityForResult(localIntent, ActivityResult);
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        //用于判断是否startActivity成功,需要xml中的配置权限才能设置成功
        super.onActivityResult(requestCode, resultCode, data);
        Log.i(TAG, "onActivityResult,resultCode:"+resultCode);
        if (-1 == resultCode) {
            Toast.makeText(this, this.getResources().getString(R.string.device_admin_jihuo_success_tips), Toast.LENGTH_LONG).show();;
        }
        else {
            Toast.makeText(this, this.getResources().getString(R.string.device_admin_jihuo_failed_tips), Toast.LENGTH_LONG).show();;
        }
    }
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:23408次
    • 积分:557
    • 等级:
    • 排名:千里之外
    • 原创:29篇
    • 转载:5篇
    • 译文:1篇
    • 评论:0条
    文章分类