Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

android - Broadcast Receiver to detect application start

I want to capture the time whenever a user starts any application using my broadcast receiver. is it possible that a broadcast receiver can catch such an event?? if yes , is there any permision which can do that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The best you can do is create a STICKY Service that tracks all of the running application.

 @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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...