Automatically prevent apps starting from the launcher Automatically prevent apps starting from the launcher android android

Automatically prevent apps starting from the launcher


Edited to elaborate on the solution

Overview

My simple solution was to add a new service and activity to my application. The service uses Handler and postDelayed to continuously schedule the monitoring task. The monitoring task checks that the current activity is on the whitelist. Getting the currently running activity involves ActivityManager and a call to getRunningTasks. After finishing the check, the monitoring task schedules itself to run again after X seconds (1, in my case).

If the activity on top is not on the whitelist, we launch the blocking activity which pops up over whatever is currently running. The key part of the blocking activity is that it overrides onBackPressed, preventing the user from simply going back to the "bad" activity. Pressing the Home key is the only way (to my knowledge) to leave this screen.

Tips

  • Build a backdoor into the lock screen. For example, my solution prompts for a password on a long-press of the back key. After entering the correct password, the monitor service goes to sleep for 5 minutes so I can do my administrative work
  • Display the name of the blocked activity
  • Gather a good whitelist before turning this on! Activities you should definitely whitelist: the launcher, package installer, your own app (obviously), the browser, if your app has a web-based component

I don't like that my service is constantly looping in the background; it seems wasteful. I'd like to find some way to be notified when a new task is being launched, but I couldn't find a way to do that. The battery usage for my particular value of the monitor period and my particular phone is acceptable; though you should definitely test before adopting this yourself.


an efective solution,and here is the code from author's opinion

public class MonitorService extends Service {private Handler handler;Runnable runnable;@Overridepublic void onCreate() {    // TODO Auto-generated method stub    super.onCreate();    handler = new Handler();    runnable = new Runnable() {        @Override        public void run() {            new Thread(new Runnable() {                @Override                public void run() {                    ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);                    List<ActivityManager.RunningTaskInfo> taskInfo = am                            .getRunningTasks(1);                    ComponentName componentInfo = taskInfo.get(0).topActivity;                                          String currentActivityName=componentInfo.getClassName();                    String packageName=componentInfo.getPackageName();                    if(whitelist.contains(currentActivityName)){                        Intent launchIntent = new Intent();                        launchIntent.setComponent(new ComponentName(blockActivityPackageName,                                blockActivityName));                        launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                        startActivity(launchIntent);                    }                }            }).start();            handler.postDelayed(this, 1000);        }    };    handler.postDelayed(runnable, 1000);}@Overridepublic void onStart(Intent intent, int startId) {    super.onStart(intent, startId);}@Overridepublic IBinder onBind(Intent intent) {    return null;}@Overridepublic void onDestroy() {    super.onDestroy();    Intent intent = new Intent(this, MonitorService.class);    startService(intent);}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {    return START_STICKY;}


As you suggest, writing a custom launcher is probably would be cleaner; check out this open source launcher for reference http://code.google.com/p/adw-launcher-android/