How to make app lock app in android?

半城伤御伤魂 提交于 2019-12-02 16:32:17

This is not how stack overflow works. You can not ask a complete solution without even trying anything.

For the most basic version of your app, you need to perform three functions.

  1. Get a list of all the installed apps on device and show them in a ListView with check box. If the user checks any app, add the app to a different list say BlockedAppsList(which will be the list of apps which user wants to block). You can get all the apps installed using the following code:

    final PackageManager pm = getPackageManager();
    //get a list of installed apps.
    List<ApplicationInfo> 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)); 
    }
    
  2. Check the which is the current opened app. You can check by using this code:

    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
        }
       }
    
  3. Now check if the current app is present in the BlockedAppsList, if it is there, you can show any screen with a block message.

good luck!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!