您可以通过以下方式获取当前正在运行的进程ActivityManager#getRunningAppProcesses http://developer.android.com/reference/android/app/ActivityManager.html#getRunningAppProcesses%28%29。但如果你的设备没有root,那么应用程序启动时绝对不可能收到通知。
当Android启动一个新应用程序时,Zygote会fork一个新进程:
static void Dalvik_dalvik_system_Zygote_forkAndSpecialize(const u4* args,
JValue* pResult)
{
pid_t pid;
pid = forkAndSpecializeCommon(args, false);
RETURN_INT(pid);
}
您可以修改并替换libdvm.so。
另一种方法 https://stackoverflow.com/a/6075480/1365960:
任何动态链接的程序都会在启动时访问某些文件,例如动态链接器。这对于安全目的来说是无用的,因为它不会在静态链接的程序上触发,但可能仍然令人感兴趣:
#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;
}
这需要root权限,因此使用JNI和root设备,您将能够做到这一点。