广播接收器检测应用程序启动

时间:2011-11-09 06:26:37

标签: android broadcastreceiver

我希望每当用户使用我的广播接收器启动任何应用程序时捕获时间。 广播接收机是否有可能捕捉到这样的事件? 如果是的话,是否有任何可以做到的倾向?

2 个答案:

答案 0 :(得分:12)

您可以做的最好的事情是创建一个跟踪所有正在运行的应用程序的STICKY服务。

 @Override
public int onStartCommand(Intent intent, int flags, int startId) 
{

    Timer timer  =  new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {

        public void run() 
        {
            final ActivityManager activityManager  =  (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
            final List<RunningTaskInfo> services  =  activityManager.getRunningTasks(Integer.MAX_VALUE);
                 for (int i = 0; i < services.size(); i++) {
                     if(!stalkList.contains(services.get(i).baseActivity.getPackageName()))
                     {
                          // you may broad cast a new application launch here.  
                          stalkList.add(services.get(i).baseActivity.getPackageName());
                     }
                }

                 List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
                for(int i = 0; i < procInfos.size(); i++) {  

                    ArrayList<String> runningPkgs = new ArrayList<String>(Arrays.asList(procInfos.get(i).pkgList));

                    Collection diff = subtractSets(runningPkgs, stalkList); 

                    if(diff != null)
                    {
                        stalkList.removeAll(diff);
                    }
               }


        }
    }, 20000, 6000);  // every 6 seconds


    return START_STICKY;
}




private RunningAppProcessInfo getForegroundApp() {
    RunningAppProcessInfo result = null, info = null;

    final ActivityManager activityManager  =  (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);

    List <RunningAppProcessInfo> l = activityManager.getRunningAppProcesses();
    Iterator <RunningAppProcessInfo> i = l.iterator();
    while(i.hasNext()) {
        info = i.next();
        if(info.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND
                && !isRunningService(info.processName)) {
            result = info;
            break;
        }
    }
    return result;
}    

private boolean isRunningService(String processName) {
    if(processName == null)
        return false;

    RunningServiceInfo service;

    final ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);

    List <RunningServiceInfo> l = activityManager.getRunningServices(9999);
    Iterator <RunningServiceInfo> i = l.iterator();
    while(i.hasNext()){
        service = i.next();
        if(service.process.equals(processName))
            return true;
    }
    return false;
}    

private boolean isRunningApp(String processName) {
    if(processName == null)
        return false;

    RunningAppProcessInfo app;

    final ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);

    List <RunningAppProcessInfo> l = activityManager.getRunningAppProcesses();
    Iterator <RunningAppProcessInfo> i = l.iterator();
    while(i.hasNext()){
        app = i.next();
        if(app.processName.equals(processName) && app.importance != RunningAppProcessInfo.IMPORTANCE_SERVICE)
            return true;
    }
    return false;
}


private boolean checkifThisIsActive(RunningAppProcessInfo target){
    boolean result = false;
    ActivityManager.RunningTaskInfo info;

    if(target == null)
        return false;

    final ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);

    List <ActivityManager.RunningTaskInfo> l = activityManager.getRunningTasks(9999);
    Iterator <ActivityManager.RunningTaskInfo> i = l.iterator();

    while(i.hasNext()){
        info=i.next();
        if(info.baseActivity.getPackageName().equals(target.processName)) {
            result = true;
            break;
        }
    }

    return result;
}  


// what is in b that is not in a ?
public static Collection subtractSets(Collection a, Collection b)
{
    Collection result = new ArrayList(b);
    result.removeAll(a);
    return result;
}

答案 1 :(得分:1)

当有人启动任何其他应用时,您无法接收广播。您唯一能做的就是启动一项服务,轮询并定期检查是否已启动新应用程序(例如,使用上面的Reno代码)。

此外,如果您在手机启动时启动此服务,则可以获得相当不错的解决方案。