问题
when my application run in background. It need to monitoring others application swaping in the foreground and get their name. How to implement? i.e. When an app goto the foreground. My app must be notified. then gets its name.
回答1:
For monitoring other Applications you can use ActivityManager for getting current top Appliction for Activity Stack as in Your Appliction:
ActivityManager am = (ActivityManager)context.getSystemService(Context. ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> processes1 = activityManager.getRunningTasks(1);
ComponentName componentInfo = processes1.get(0).topActivity;
String classname =processes1.get(0).topActivity.getClassName();
String packagename = processes1.get(0).topActivity.getPackageName();
回答2:
we can’t use getRunningTasks because android specifies “method is only intended for debugging and presenting task management user interfaces“.
There is no direct approach to get the app status while background or foreground, but there is possibility of getting to know when app is foreground and background For more details: See here
来源:https://stackoverflow.com/questions/10826579/how-to-monitoring-app-swaping-in-foreground