Android background service for defining a foreground application

I am working on an application that will track phone usage throughout the day. To do this, I have a help desk that starts by loading the device and continues the survey to find out what the current foreground application is. The following code works when I just click on an application and then exit it and click on another application. Now let's say I open the browser and go to another web page, and it stops registering the browser as a foreground application. Or, for example, I open google talk, it registers it as current activity, but when I send a message, it will no longer register it as a foreground application. Any thoughts or ideas on what might be causing this error? If necessary, I provided any additional information, just let me know.Perhaps the answer lies with this post, and I just can't find it ...Identify the current 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;
}
+3
source share
2 answers

found the answer, here is how I did it. I just call getForegroundApp () from the 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 that this is not the best method, especially since I determine that the launcher is not open, because there are more launchers than just the ones I have listed. But this works for my use case, so I thought I would share.

+1
source

Below code works for me. The first task on the list is that I need to determine the name of the foreground application.

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

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

All Articles