How do I know when another application starts running?

advertisements

Possible Duplicate:
Android:how does application Protector app works?

How can i be notified when new app begin launching? clearly to say, i click on app icon , and app begin launching, i want to set my service to observer of that event (if there is any). Is there any event or some way to know that before app launched ?


If I were you, I would get the ActivityManager with a call to

getSystemService(Context.ACTIVITY_SERVICE)

and then setup my program with the following code:

private final static Handler updateHandler = new Handler();
private static long WAKEUP_INTERVAL = 10000;    // 10secs?

private Runnable periodicUpdate = new Runnable() {
    @Override
    public void run() {
        checkRunningProcesses();
        updateHandler.postDelayed(this, WAKEUP_INTERVAL);
    }
};

to do periodical checks of the following two things:

  1. check getRecentTasks() to find out what user has launched recently (requres GET_TASKS permission)
  2. get from getRunningAppProcesses() and keep somewhere list of running processes just in case periodical check interval has been chosen too long and some processes were added/removed but did not show in getRecentTasks()

This way I could not get an instant notification about program launch, but could find about that soon enough (seconds later, maybe) to do something about it.

One more thing, it might be a wise idea to stop checks when screen goes dark, to save the battery.