在Android上向现有设备管理员添加新的使用策略

时间:2015-07-09 13:26:06

标签: android android-manifest android-permissions android-securityexception

我有一个设备管理员应用程序,它使用以下device-admin.xml

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

某些用户已经激活了设备管理员权限。现在,在更新应用程序时,我想添加一个新的使用策略

 <limit-password />

我想知道如何检测已经以编程方式添加了新的使用策略,以便我们推动重新激活设备管理员权限?

2 个答案:

答案 0 :(得分:1)

AFAIK唯一的方法是从你的apk中读取你的device-admin.xml,你可以这样做(来自一个Activity):

    PackageManager packageManager = getPackageManager();
    List <PackageInfo> packageInfoList = packageManager.getInstalledPackages(0);
    for (PackageInfo packageInfo : packageInfoList) {
        String publicSourceDir = packageInfo.applicationInfo.publicSourceDir;
        if (publicSourceDir.contains("your/app/path")) { // or equals, which you prefer
            File apkFile = new File(publicSourceDir);
            if (apkFile.exists()) {
                try {
                    JarFile jarFile = new JarFile(apkFile);
                    JarEntry jarEntry = jarFile.getJarEntry("xml/device-admin.xml");
                    InputStream is = jarFile.getInputStream(jarEntry);
                    BufferedReader br = new BufferedReader(new InputStreamReader(is));
                    String str;
                    while ((str = br.readLine()) != null) {
                        Log.d("entry: ", str); // your entries in the device-admin.xml
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            break;
        }
    }

答案 1 :(得分:0)

要检查您的活动管理员是否已授予政策,您可以使用 https://developer.android.com/reference/android/app/admin/DevicePolicyManager#hasGrantedPolicy(android.content.ComponentName,%20int)

要验证我喜欢使用的device-admin.xml中的使用策略 https://developer.android.google.cn/reference/android/app/admin/DeviceAdminInfo.html?hl=zh-cn#usesPolicy(int) 但是当usesPolicy返回true时,它并不代表主动管理员可以使用它。

ComponentName componentName = new ComponentName(context, MyDeviceAdminReceiver.class);
ResolveInfo resolveInfo = new ResolveInfo();
resolveInfo.activityInfo = context.getPackageManager().getReceiverInfo(componentName, PackageManager.GET_META_DATA);
DeviceAdminInfo info = new DeviceAdminInfo(context, resolveInfo);

if (info.usesPolicy(DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD)) {
    //your application declared <limit-password/> in device_admin.xml
}

DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
if (dpm.hasGrantedPolicy(componentName, DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD)) {
    //active device admin has granted <limit-password/> policy
}