开发者

Android background service to determine foreground application

开发者 https://www.devze.com 2023-03-13 02:03 出处:网络
I\'m working on an application that will monitor the phone usage throughout the day. To do this I have a background service that starts on device boot and keeps on polling to find out what the current

I'm working on an application that will monitor the phone usage throughout the day. To do this I have a background service that starts on device boot and keeps on polling to find out what the current foreground application is. The following code works when I am just clicking an application then backing out of it and clicking another application. Now lets say I open the browser and go to a different webpage, it will stop registering the browser as being the foreground application. Or for example, I open up google talk, it will register it as the current activity, but when I send a message it will no longer register it as the foreground application. Any thoughts or ideas as to what might be the cause of this bug? If needed I'll provide any additional info, just let me know. Maybe the answer lies in this post and I just can't find it... Determining the curren开发者_Python百科t foreground application from a background task or service

public static RunningAppProcessInfo getForegroundApp() throws NameNotFoundException{
    RunningAppProcessInfo result = null, info = null;
    String currApp = null;
    am = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE);
    Drawable icon = null;
List <RunningAppProcessInfo> l = am.getRunningAppProcesses();
Iterator <RunningAppProcessInfo> i = l.iterator();
PackageManager pm = context.getPackageManager();

while(i.hasNext()){
    info = i.next();
        if(info.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && getActivityForApp(info)!=null ){

            try {
                currApp = getCurrentApplication(info);
                System.out.println(currApp);
                System.out.println(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
                icon = pm.getApplicationIcon(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
                System.out.println(getActivityForApp(info));
            } catch (NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
        }
    break;       
}

return result;

}

private static ComponentName getActivityForApp(RunningAppProcessInfo target){
    ComponentName result=null;
    ActivityManager.RunningTaskInfo info;

    if(target==null)
        return null;

    if(am==null)
        am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
    List <ActivityManager.RunningTaskInfo> l = am.getRunningTasks(9999);
    Iterator <ActivityManager.RunningTaskInfo> i = l.iterator();

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


Below code works for me. The first task in the list is what I need to determine the foreground application name.

ActivityManager activityManager = (ActivityManager) getSystemService(UpdateService.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> appProcesses = activityManager.getRunningTasks(1);

Log.i("MyApp", "Foreground Package: " + appProcesses.get(0).topActivity.getPackageName());


found the answer, heres how I did it. I am just calling getForegroundApp() from a background service.

   public String getForegroundApp() throws NameNotFoundException{ 

        RunningTaskInfo info = null;
        ActivityManager am;
        am = (ActivityManager)mContext.getSystemService(ACTIVITY_SERVICE);
        List<RunningTaskInfo> l = am.getRunningTasks(1000);
        System.out.println(l);
        Iterator <RunningTaskInfo> i = l.iterator();


        String packName = new String();
        String appName = new String();
        ApplicationInfo appInfo = new ApplicationInfo();

        while(i.hasNext()){
            info = i.next();
            packName = info.topActivity.getPackageName();
            if(!packName.equals("com.htc.launcher") && !packName.equals("com.android.launcher")){ //this could be what is causing the problem. not sure.
                packName = info.topActivity.getPackageName();
                break;
            }
            info = i.next();
            packName= info.topActivity.getPackageName();
            break;          
            }
        return packName;
        }

I know this isn't the best method, especially how I am determining that the launcher is not open since there are more launchers out there than just the two I have listed. But this works for my use case so I thought I would share.


This is an old thread, but I just want to add a note here that getRunningsTasks() is deprecated in API level 21, so solutions above using getRunningsTasks() will not work. See

https://developer.android.com/reference/android/app/ActivityManager

getRunningTasks(int maxNum) - This method was deprecated in API level 21. As of Build.VERSION_CODES.LOLLIPOP, this method is no longer available to third party applications: the introduction of document-centric recents means it can leak person information to the caller. For backwards compatibility, it will still return a small subset of its data: at least the caller's own tasks, and possibly some other tasks such as home that are known to not be sensitive.

0

精彩评论

暂无评论...
验证码 换一张
取 消