检测当其他应用程序中打开或启动

检测当其他应用程序中打开或启动

问题描述:

我想跟踪或当用户试图打开像Facebook或雅虎或Gmail或任何其他应用程序的移动应用程序一检测。 例如: - 要知道这些都是用户在最后30分钟已经打开的应用程序

I would like to track or detect when the user tries to open a application in the mobile like facebook or yahoo or gmail or any other application. Eg:- To know these are the application user has opened in last 30 minutes.

不能检测应用推出的Andr​​oid。但你可以得到当前打开的应用程序列表中使用该code和检查,如果你正在寻找的应用程序是开放与否:

You cannot detect an App launch in Android. But you can get the list of currently open apps using this code and check if the app you're looking for is open or not:

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

for (int i = 0; i < runningAppProcessInfo.size(); i++) {
  if(runningAppProcessInfo.get(i).processName.equals("com.the.app.you.are.looking.for") {
    // Do you stuff
  }
}

您也可以检查,如果应用程序在前台使用这种方法运行

You can also check if the app is running in the foreground using this method

public static boolean isForeground(Context ctx, String myPackage){
    ActivityManager manager = (ActivityManager) ctx.getSystemService(ACTIVITY_SERVICE);
    List< ActivityManager.RunningTaskInfo > runningTaskInfo = manager.getRunningTasks(1); 

    ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
    if(componentInfo.getPackageName().equals(myPackage)) {
        return true;
    }       
    return false;
}