3

我有一个使用以下 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 />

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

4

2 回答 2

1

AFAIK 的唯一方法是从您的 apk 中读取您的 device-admin.xml,您可以通过这种方式(从活动)执行此操作:

    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;
        }
    }
于 2016-07-04T15:17:21.457 回答
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) 但是当usesPolicyreturntrue它并不意味着活动管理员可以使用它。

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
}
于 2018-06-01T09:19:19.893 回答