How to create an app locker in Android Studio
Get list of all installed app by following code.
- final PackageManager pm = getPackageManager();
- //get a list of installed apps.
- List packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
- for (ApplicationInfo packageInfo : packages) {
- Log.d(TAG, "Installed package :" + packageInfo.packageName);
- Log.d(TAG, "Source dir : " + packageInfo.sourceDir);
- Log.d(TAG, "Launch Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName));
- }
Check for which app is opened currently
- ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
- List l = am.getRecentTasks(1, ActivityManager.RECENT_WITH_EXCLUDED);
- Iterator i = l.iterator();
- PackageManager pm = this.getPackageManager();
- while (i.hasNext()) {
- ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
- try {
- CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(
- info.processName, PackageManager.GET_META_DATA));
- Log.w("LABEL", c.toString());
- } catch (Exception e) {
- // Name Not FOund Exception
- }
- }
Now check if the current app is present in the BlockedAppsList, if it is there, you can show any screen with a block message.
You need to create service which will run in background.