1

Google API 允许我们通过 getActiveAdmins() 方法获取设备中所有活动管理员的列表。但是,我的要求是我想要设备中所有可能的管理员的列表,无论是否处于活动状态。有什么方法可以做到吗?

谢谢

4

2 回答 2

2

您可以通过以下代码找到应用程序列表

final Intent deviceAdminIntent = new Intent("android.app.action.DEVICE_ADMIN_ENABLED", null);
final List<ResolveInfo> pkgAppsList = getPackageManager().queryBroadcastReceivers(deviceAdminIntent, 0);
for (ResolveInfo aResolveInfo : pkgAppsList) {
    String pkg = aResolveInfo.activityInfo.applicationInfo.packageName;
        String name = aResolveInfo.activityInfo.applicationInfo.loadLabel(getPackageManager()).toString();

    System.out.println("Package :: " + pkg);
    System.out.println("Name :: " + name);
}

您可以在应用程序的 ResolveInfo 中获取所有必要的数据。您可以在此处查看 ResolveInfo javadoc 。


我写了一个方法,它返回与getActiveAdmins相同的ComponentName

private List<ComponentName> getAllAdmins(Context mContext) {
        List<ComponentName> result = new ArrayList<ComponentName>();

        // Read all receivers who can listen android.app.action.DEVICE_ADMIN_ENABLED 
        // You can add all other action which can be used for DeviceAdminReceiver 
        final Intent deviceAdminIntent = new Intent("android.app.action.DEVICE_ADMIN_ENABLED", null);
        final List<ResolveInfo> pkgAppsList = mContext.getPackageManager().queryBroadcastReceivers(deviceAdminIntent, 0);
        for (ResolveInfo aResolveInfo : pkgAppsList) {
            // Prepare component and add to list

            result.add(new ComponentName(aResolveInfo.activityInfo.applicationInfo.packageName,
                    aResolveInfo.activityInfo.name)); 

            //String pkg = aResolveInfo.activityInfo.applicationInfo.packageName;
            //String name = aResolveInfo.activityInfo.applicationInfo.loadLabel(getPackageManager()).toString();
            //System.out.println("Package :: " + pkg);
            //System.out.println("Name :: " + name);
        }
        return result; 
    }
于 2013-10-08T06:37:14.740 回答
2

您可以在以下 Android 源代码中找到 DeviceAdminSettings 的必要代码:https ://android.googlesource.com/platform/packages/apps/Settings/+/kitkat-release/src/com/android/settings/DeviceAdminSettings.java

DevicePolicyManager mDPM; // = (DevicePolicyManager) getActivity().getSystemService(Context.DEVICE_POLICY_SERVICE);
final HashSet<ComponentName> mActiveAdmins = new HashSet<ComponentName>();
final ArrayList<DeviceAdminInfo> mAvailableAdmins = new ArrayList<DeviceAdminInfo>();

...

    mActiveAdmins.clear();
    List<ComponentName> cur = mDPM.getActiveAdmins();
    if (cur != null) {
        for (int i=0; i<cur.size(); i++) {
            mActiveAdmins.add(cur.get(i));
        }
    }
    mAvailableAdmins.clear();
    List<ResolveInfo> avail = getActivity().getPackageManager().queryBroadcastReceivers(
            new Intent(DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED),
            PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS);
    if (avail == null) {
        avail = Collections.emptyList();
    }
    // Some admins listed in mActiveAdmins may not have been found by the above query.
    // We thus add them separately.
    Set<ComponentName> activeAdminsNotInAvail = new HashSet<ComponentName>(mActiveAdmins);
    for (ResolveInfo ri : avail) {
        ComponentName riComponentName =
                new ComponentName(ri.activityInfo.packageName, ri.activityInfo.name);
        activeAdminsNotInAvail.remove(riComponentName);
    }
    if (!activeAdminsNotInAvail.isEmpty()) {
        avail = new ArrayList<ResolveInfo>(avail);
        PackageManager packageManager = getActivity().getPackageManager();
        for (ComponentName unlistedActiveAdmin : activeAdminsNotInAvail) {
            List<ResolveInfo> resolved = packageManager.queryBroadcastReceivers(
                    new Intent().setComponent(unlistedActiveAdmin),
                    PackageManager.GET_META_DATA
                            | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS);
            if (resolved != null) {
                avail.addAll(resolved);
            }
        }
    }
    for (int i = 0, count = avail.size(); i < count; i++) {
        ResolveInfo ri = avail.get(i);
        try {
            DeviceAdminInfo dpi = new DeviceAdminInfo(getActivity(), ri);
            if (dpi.isVisible() || mActiveAdmins.contains(dpi.getComponent())) {
                mAvailableAdmins.add(dpi);
            }
        } catch (XmlPullParserException e) {
            Log.w(TAG, "Skipping " + ri.activityInfo, e);
        } catch (IOException e) {
            Log.w(TAG, "Skipping " + ri.activityInfo, e);
        }
    }

    getListView().setAdapter(new PolicyListAdapter());

这就是 Android 在“设备管理员”选项卡中为您列出它们的方式。

于 2015-03-19T08:41:53.580 回答