Blocking android apps programmatically

若如初见. 提交于 2019-11-28 02:44:10
Amit Gupta

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.

Looks like this is still a mystery as suggested by above mentioned comments.So I'm putting the code that helped me to solve this problem.

getForegroundApp

public String getForegroundApp() {
    String currentApp = "NULL";
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        UsageStatsManager usm = (UsageStatsManager) this.mContext.getSystemService(Context.USAGE_STATS_SERVICE);
        long time = System.currentTimeMillis();
        List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 1000, time);
        if (appList != null && appList.size() > 0) {
            SortedMap<Long, UsageStats> mySortedMap = new TreeMap<Long, UsageStats>();
            for (UsageStats usageStats : appList) {
                mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
            }
            if (mySortedMap != null && !mySortedMap.isEmpty()) {
                currentApp = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
            }
        }
    } else {
        ActivityManager am = (ActivityManager) this.mContext.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> tasks = am.getRunningAppProcesses();
        currentApp = tasks.get(0).processName;
    }

    return currentApp;
}

Call getForegroundApp() and it will return a string which contains the name of the currentForegroundApp including the package name e.g. com.example.app

Now, to use this code, we need this line of code in Manifest file

    <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" />

and take user to Usage Data access Settings:

usageAccessSettingsPage

  public void usageAccessSettingsPage(){
    Intent intent = new Intent();
    intent.setAction(Settings.ACTION_USAGE_ACCESS_SETTINGS);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    Uri uri = Uri.fromParts("package", mContext.getPackageName(), null);
    intent.setData(uri);
    startActivity(intent);
}

or manually by finding in LockScreen and Security> Other security settings> Usage access data.

Now comes the part for blocking the app, this part is covered in Amit's answer very well. However, if someone is looking for a way to restrict user from using an app then a trick is to open home screen when a particular app is launched.

This could be done by calling the following method when the currentApp is equal to a blocked app

if(<NameOfBlockedApp>.equals currentApp){
     showHomeScreen();
   }

ShowHomeScreen

 public boolean showHomeScreen(){
    Intent startMain = new Intent(Intent.ACTION_MAIN);
    startMain.addCategory(Intent.CATEGORY_HOME);
    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    mContext.startActivity(startMain);
    return true;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!