Suspend Apps Using Package Manager Android Studio

102 Views Asked by At

How can I suspend all apps on my fully managed device?

I've already tried using ResolveInfo and queryIntentActivity, but it only suspends some of the basic user apps (camera, gallery, calendar, etc.), and not apps like Spotify, YouTube, Netflix, Play Store, Gmail, etc. Is there a solution to this problem?

Here is the code I've been using to suspend apps except for some allowed ones:

private void suspendAllAppsExceptAllowedApps(DevicePolicyManager dpm, ComponentName adminComponent) {
    PackageManager pm = getPackageManager();
    List<String> exemptPackages = Arrays.asList("com.android.settings", "com.example.MyAppsSuspend", "com.android.contacts", "com.android.phone");
    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    List<ResolveInfo> packages = pm.queryIntentActivities(mainIntent, 0);

    for (ResolveInfo resolveInfo : packages) {
        try {
            String package_name = resolveInfo.activityInfo.packageName;
            if (!exemptPackages.contains(package_name)) {
                String[] suspended = dpm.setPackagesSuspended(adminComponent, new String[]{package_name}, true);
                if (suspended != null) {
                    // Log.i("AppSuspend", "Package suspended: " + suspended);
                } else {
                    Log.i("AppSuspend", "Failed to suspend package: " + package_name);
                }
            }
        } catch (Exception e) {
            Log.e("AppSuspend", "Error: " + e.getMessage());
        }
    }
}
0

There are 0 best solutions below