How to stop opening default apps in android?

I am trying to make an android app for users in which user can activate Child protection. for example, User opens my app, he can view all the apps he has (including default ones). Now if the user blocks default message or calls app then everything should be same, just these selected app must not open, even after anyone tap on these applications multiple time. It should look like nothing happening. I don't want to have a privacy PIN code or pattern on that app. I just want to stop the children by opening any app of mobile selected by the user through my application. Is this possible? if yes then any idea about how can i achieve this thing?

1 answer

  • answered 2018-07-12 08:05 Simone Chelo

    This problem is very similar to the one answered here, that will be my source for this answer. I can't try this code, but I hope it can give you a rough idea to make your first try and see yourself if it work or not.

    The idea

    The basic idea is to create a Background Service that continuously checks if there are open apps. If those apps are on the list the user locked, the show a lock screen. I don't know if it is possible to close an opened app¹ (I hope it's not!), but for your parental control functionality, a lock screen should have the same effect.

    Select the apps to lock.

    To retrieve from the system all apps that are installed

    PackageManager packageManager = getPackageManager();
    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    
    List<ResolveInfo> appList = packageManager.queryIntentActivities(mainIntent, 0);
    Collections.sort(appList, new ResolveInfo.DisplayNameComparator(packageManager));
    List<PackageInfo> packs = packageManager.getInstalledPackages(0);
    for (int i = 0; i < packs.size(); i++) {
        PackageInfo p = packs.get(i);
        ApplicationInfo a = p.applicationInfo;
        /* Uncomment this to exclude system apps
        if ((a.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
            continue;
        } */
        appList.add(p.packageName);
    }
    

    Now appList is a List<ResolveInfo>, and you can show to the user a ListView to let her choose the apps to be locked. Save these somewhere (SharedPreferences is an idea).

    The service

    What the service has to do is to:

    Check the currently opened app:

    ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1);
    ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
    activityOnTop = ar.topActivity.getClassName();
    

    Check if activityOnTop is in the locked-apps list.

    for(String appName : appList) {
        if(appName.equals(activityOnTop)) {
            // This app is locked!
            LockApp(); // Defined later
            break;
        }
    }
    

    If so, start an Intent to your LockScreen activity (that won't allow any action)

    private void LockApp() {
       Intent lockIntent = new Intent(mContext, LockScreen.class);
       lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       mContext.startActivity(lockIntent);
    }
    

    I give a lot of things for granted: you have to start a Service somewhere, you have to build a couple of activities, and this process does not avoid the problem of a smart kid that closes the process in background ... But I think this can work as a starting point.

    ¹ It apparently is possible. Give a look to this answer, an idea is to substitute the code in LockApp() with something like that. This doesn't change that I think a Lock Screen is more elegant.