hharry
hharry

Reputation: 412

Is it possible to get notification when any app is start in android device

I want that when I start any application in device then it should notify me (programmatically). Is it possible to get notification of any app when run(launch).

Upvotes: 4

Views: 832

Answers (2)

StarPinkER
StarPinkER

Reputation: 14281

You can get the current running process by ActivityManager#getRunningAppProcesses. But it is definitely impossible to get notified when application started without get your device rooted.

When Android starts a new application, Zygote will fork a new process:

static void Dalvik_dalvik_system_Zygote_forkAndSpecialize(const u4* args,
    JValue* pResult)
{
    pid_t pid;

    pid = forkAndSpecializeCommon(args, false);

    RETURN_INT(pid);
}

You can modify and replace the libdvm.so.

Another approach:

Any program which is dynamically linked is going to access certain files on startup, such as the dynamic linker. This would be useless for security purposes since it won't trigger on a statically linked program, but might still be of interest:

#include <stdio.h>
#include <sys/inotify.h>
#include <assert.h>
int main(int argc, char **argv) {
    char buf[256];
    struct inotify_event *event;
    int fd, wd;
    fd=inotify_init();
    assert(fd > -1);
    assert((wd=inotify_add_watch(fd, "/lib/ld-linux.so.2", IN_OPEN)) > 0);
    printf("Watching for events, wd is %x\n", wd);
    while (read(fd, buf, sizeof(buf))) {
      event = (void *) buf;
      printf("watch %d mask %x name(len %d)=\"%s\"\n",
         event->wd, event->mask, event->len, event->name);
    }
    inotify_rm_watch(fd, wd);
    return 0;
}

This requires root privilege, so with JNI and a rooted device, you will be able to do this.

Upvotes: 3

Raghav Sood
Raghav Sood

Reputation: 82583

No, this is not really possible using the public SDK.

At best, you could constantly keep polling the ActivityManager for the foreground process that's right on top, and keep a log of that. However, it isn't the most accurate, or efficient method.

Upvotes: 2

Related Questions