I tried to develop such an app, in the sense I want to lock all the applications in my device with a password whatever I want. But I didn't find any piece of code for the solution. So I developed one by myself and unfortunately it didn't succeed. I found many solutions for locking android devices but, didn't find one for locking an app. Will be glad if you suggest a solution.
How to Select Apps. To choose the apps you wish to block, tap the Blocklists icon on the bottom of the screen and then tap "Manage" next to Blocked Applications. After tapping "Manage", you'll see a list of your device's apps divided by category.
To turn on the parental controls in the Google Play Store, open the store on the device and then tap the 3 lines in the top left corner of the screen. Next tap “settings” and then “Parental controls”. Turn it on by toggling the switch to the On position. Tap each area to set restrictions for that particular item.
Select Pattern, Pin, or Password (or a biometric option, if available), then continue by entering your selection and confirming it. Select Secure Folder from the app drawer, then tap Add apps. Select the apps you wish to include in Secure Folder, then tap Add. Select Lock and exit in the upper-right corner.
I used a background service to check which application is in the foreground (which means that application is being used by the user). Then I check to see whether I need to lock the application or not.
To find the list of all installed applications (excluding system applications):
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; // skip system apps if they shall not be included if ((a.flags & ApplicationInfo.FLAG_SYSTEM) == 1) { continue; } appList.add(p.packageName); }
To find the current foreground application:
ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1); ActivityManager.RunningTaskInfo ar = RunningTask.get(0); activityOnTop=ar.topActivity.getClassName();
Here the class-name provides the package name of the application. I suggest that you use the package name to identify any application so we know that the package name is always unique.
Now, the functionality to lock the application:
To find which application is running in the foreground and want to lock it, we just have to start another activity which has an EditText for password and OK and Cancel button.
Intent lockIntent = new Intent(mContext, LockScreen.class); lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); mContext.startActivity(lockIntent);
On click of OK, if the password is correct then simply finish the LockScreen activity. If the password is incorrect then simply use the code below, which closes the application and shows the home screen of the device:
Intent startHomescreen = new Intent(Intent.ACTION_MAIN); startHomescreen.addCategory(Intent.CATEGORY_HOME); startHomescreen.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(startHomescreen);
The same code is also used on the cancel button.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With