android典型代码系列(五)------deviceadmin步骤

本文详细介绍了在Android中实现设备管理员(DeviceAdmin)的步骤,包括创建DeviceAdminReceiver子类,配置my_admin.xml文件,以及如何注册广播接收器为设备管理员。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2_deviceadmin步骤 :
1.创建 MyAdmin 的广播接受者 继承 DeviceAdminReceiver

<receiver android:name=".MyAdmin">
            <meta-data android:name="android.app.device_admin"
                android:resource="@xml/my_admin" />
            <intent-filter>
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
            </intent-filter>
        </receiver>

my_admin.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 />
        </uses-policies>
</device-admin>

2.获取IDevicePolicyManager

Method method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
IBinder binder = (IBinder) method.invoke(null,new Object[] { Context.DEVICE_POLICY_SERVICE });
mService = IDevicePolicyManager.Stub.asInterface(binder);

3.注册广播接受者为admin设备

ComponentName mAdminName = new ComponentName(this, MyAdmin.class);
if (mService != null) {
        if (!mService.isAdminActive(mAdminName)) {
                    Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,mAdminName);
                    startActivity(intent);
                }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值