How to programmatically set a lock or pin for an app

String lastAppPN = "";
public void checkRunningApps() {
    ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    String activityOnTop;
    if (Build.VERSION.SDK_INT > 20) {
        activityOnTop = mActivityManager.getRunningAppProcesses().get(0).processName;
    } else {
        List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1);
        ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
        activityOnTop = ar.topActivity.getPackageName();
    }
    //Log.e("activity on TOp", "" + activityOnTop);

    // Provide the packagename(s) of apps here, you want to show password activity
    if (activityOnTop.contains("whatsapp")  // you can make this check even better
            || activityOnTop.contains(CURRENT_PACKAGE_NAME)) {
        if (!(lastAppPN.equals(activityOnTop))) {
            lastAppPN = activityOnTop;
            Log.e("Whatsapp", "started");
        }
    } else {
        if (lastAppPN.contains("whatsapp")) {
            if (!(activityOnTop.equals(lastAppPN))) {
                Log.e("Whatsapp", "stoped");
                lastAppPN = "";
            }
        }
        // DO nothing
    }
}

I have created small demo project. Hope this might be useful to someone Link to project


Logic

  • You have to make and start a service when you want to block apps,
  • And In Service you have to check packagenames of the apps, so that you can decide which app to run and which to show a pin/password activity

Now Code Example:

  • To Start a service, code like this,

    startService(new Intent(this, SaveMyAppsService.class));
    
  • Now, Inside your service, check packages like this,

    public class SaveMyAppsService extends android.app.Service 
    {
    
        String CURRENT_PACKAGE_NAME = {your this app packagename};
        String lastAppPN = "";
        boolean noDelay = false;
        public static SaveMyAppsService instance;
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            // TODO Auto-generated method stub
    
            scheduleMethod();
            CURRENT_PACKAGE_NAME = getApplicationContext().getPackageName();
            Log.e("Current PN", "" + CURRENT_PACKAGE_NAME);
    
            instance = this;
    
            return START_STICKY;
        }
    
        private void scheduleMethod() {
            // TODO Auto-generated method stub
    
            ScheduledExecutorService scheduler = Executors
                    .newSingleThreadScheduledExecutor();
            scheduler.scheduleAtFixedRate(new Runnable() {
    
                @Override
                public void run() {
                    // TODO Auto-generated method stub
    
                    // This method will check for the Running apps after every 100ms
                    if(30 minutes spent){
                         stop();
                    }else{
                       checkRunningApps();
                   }
                }
            }, 0, 100, TimeUnit.MILLISECONDS);
        }
    
        public void checkRunningApps() {
            ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1);
            ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
            String activityOnTop = ar.topActivity.getPackageName();
            Log.e("activity on TOp", "" + activityOnTop);
    
            // Provide the packagename(s) of apps here, you want to show password activity
        if (activityOnTop.contains("whatsapp")  // you can make this check even better
                || activityOnTop.contains(CURRENT_PACKAGE_NAME)) {
                // Show Password Activity                
            } else {
                // DO nothing
            }
         }
    
        public static void stop() {
            if (instance != null) {
            instance.stopSelf();
            }
        }
    }   
    

Edit: (Get Top Package Name for Lollipop)

A very good answer is here.