检测当其他应用程序中打开或启动 [英] Detect When other Application opened or Launched

查看:135
本文介绍了检测当其他应用程序中打开或启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想跟踪或当用户试图打开像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;
}

这篇关于检测当其他应用程序中打开或启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆