Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blocking android apps programmatically

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.

like image 354
thampi joseph Avatar asked Nov 08 '13 05:11

thampi joseph


People also ask

How do I block specific apps on Android?

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.

How do I block a specific app from installing?

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.

How do you lock an app on an app?

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.


1 Answers

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.

like image 134
Amit Gupta Avatar answered Sep 22 '22 22:09

Amit Gupta