How to programmatically set a lock or pin for an app

丶灬走出姿态 提交于 2019-11-29 21:08:09
Nadeem Iqbal

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.

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