简体   繁体   English

检测其他应用程序何时打开或启动

[英]Detect When other Application opened or Launched

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. 我想跟踪或检测用户何时尝试在移动设备中打开应用程序,如facebook或yahoo或gmail或任何其他应用程序。 Eg:- To know these are the application user has opened in last 30 minutes. 例如: - 要知道这些是应用程序用户在过去30分钟内打开的。

You cannot detect an App launch in Android. 无法在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;
}

This library tries to provide an easy way to get the foreground application package name. 该库试图提供一种获取前台应用程序包名称的简便方法。 It uses different techniques, adequate to the device's android version. 它使用不同的技术,足以满足设备的Android版本。

https://github.com/ricvalerio/foregroundappchecker https://github.com/ricvalerio/foregroundappchecker

With the help of @aaRBiyecH's answer, I have created a service which can detect if other application launches. 在@ aaRBiyecH的答案的帮助下,我创建了一个服务,可以检测其他应用程序是否启动。 In this example, the service detects for the Android Dialer app ( com.android.dialer ): 在此示例中,服务检测到Android Dialer应用程序( com.android.dialer ):

@Override
public int onStartCommand(Intent intent, int flags, int startId){
    private static final String TAG = "com.myapp.Service"; // Replace this with your actual class' name
    Timer timer  =  new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {
        boolean isDialerAppLaunched = false, isDialerAppClosed = false;
        int dialerAppLaunches = 0;
        @Override
        public void run() {
            ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();

            for ( ActivityManager.RunningAppProcessInfo appProcess: runningAppProcessInfo ) {
                Log.d(appProcess.processName.toString(),"is running");
                if (appProcess.processName.equals("com.android.dialer")) {
                    if ( appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND){
                        if (!isDialerAppLaunched){
                            isDialerAppLaunched = true;
                            Log.d(TAG, "Dialer app has been launched");
                        }
                        else if (isDialerAppClosed){
                            phonelaunches++;
                            isDialerAppClosed = false;
                            Log.d("Dialer app has been launched " + String.valueOf(phonelaunches) + "times.");
                        }
                    }
                    else {
                        isDialerAppClosed = true;
                        Log.d(str,"Dialer app has been closed.");

                    }
                }
            }
        }
    },2000,3000);

    return START_STICKY;
}

Here I go through all the running tasks and check if it is our intended application. 在这里,我将完成所有正在运行的任务并检查它是否是我们的预期应用程序。 If so, I check if the application is in the foreground and application is never launched using the isDialerAppLaunched variable. 如果是这样,我检查应用程序是否在前台,并且永远不会使用isDialerAppLaunched变量启动应用程序。 isDialerAppClosed is used when the intended application is in the background and the variable is set accordingly. 当预期的应用程序在后台并且相应地设置变量时,使用isDialerAppClosed

All this is implemented in a Service class. 所有这些都在Service类中实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM