문제

I want to implement an app which can detect what app launched, to do something with that!

for example I have a list of installed applications in my app and mark one favorite app. and when I start that marked app from default launcher or anyway, I could detect it and do something with that by a background service or broadcast receiver(For example launch a toast message).

How can I do this?

도움이 되었습니까?

해결책

this isnt possible.. to be able to monitor all intents would make android extremely insecure

http://groups.google.com/group/andro...ddc9d36a24d77b

but there are ways to know when an application is launched. you just have to be creative. this will give you a list of all the applications running.

Code:

ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();

however to know when an app is launched you would need a timed loop and then check between versions of the List to see if there is a new app. this would suck the juice and be inneficient

AppProtector seem to access the eventlog. maybe you could have a ContentObserver attached to the event log http://developer.android.com/referen.../EventLog.html http://developer.android.com/referen...tObserver.html

EDIT

Interesting. I also found this which solve your problem.

When you open any app from launcher below code will return the info of opened app so now you need to compare a package name with your favourite app package name which you already stored in your app database.

Code:

String str = ((ActivityManager.RunningTaskInfo)this.am.getRunningTasks(1).get(0)).topActivity.getPackageName();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top