thampi joseph thampi joseph - 1 year ago 121
Android Question

Blocking android apps programatically

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. Thank You

Answer Source

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 whether I need to lock the application or not.

To find the list of all installed application, excluding system application:

PackageManager packageManager = getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);

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) {

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);

Here the class name provides the package name of the application. I suggest you use the package name to identify any application so we know that the package name is always unique.

Now functionality to lock the application

When we 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);

On click of Ok if the password is correct then simply finish the LockScreen activity. If the password is incorrect then simply use the below mention code which closes the application and shows the home screen of the device:

Intent startHomescreen=new Intent(Intent.ACTION_MAIN);

The same code is also used on the cancel button.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download