Android的后台服务,以确定前台应用程序 [英] Android background service to determine foreground application

查看:179
本文介绍了Android的后台服务,以确定前台应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个应用程序,将监视全天电话的使用量。要做到这一点我有开始对设备启动和不断轮询来找出当前前台应用程序是一个后台服务。下面code时,我只需点击一个应用程序,然后打了退堂鼓的它并单击另一个应用程序的工作原理。现在可以说,我打开浏览器,进入到不同的网页,就会停止注册浏览器是前台应用程序。或者,例如,我打开谷歌对话,将其注册为当前活动,但是当我发送邮件将不再把它注册为前台应用程序。任何想法或想法,什么可能是这个错误的原因是什么?如果需要的话,我会提供任何额外的信息,只是让我知道。
也许答案就在这个岗位,我只是无法找到它... <一个href=\"http://stackoverflow.com/questions/2166961/determining-the-current-foreground-application-from-a-background-task-or-service\">Determining从后台任务或服务当前前台应用程序

 公共静态RunningAppProcessInfo getForegroundApp()抛出{的NameNotFoundException
    RunningAppProcessInfo结果= NULL,资讯= NULL;
    字符串currApp = NULL;
    AM =(ActivityManager)context.getSystemService(ACTIVITY_SERVICE);
    可绘制图标= NULL;
清单&LT; RunningAppProcessInfo&GT; L = am.getRunningAppProcesses();
迭代器&LT; RunningAppProcessInfo&GT; I = l.iterator();
软件包管理系统下午= context.getPackageManager();而(i.hasNext()){
    信息= i.next();
        如果(info.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND&放大器;&安培;!getActivityForApp(INFO)= NULL){            尝试{
                currApp = getCurrentApplication(信息);
                的System.out.println(currApp);
                的System.out.println(pm.getApplicationInfo(info.processName,PackageManager.GET_META_DATA));
                图标= pm.getApplicationIcon(pm.getApplicationInfo(info.processName,PackageManager.GET_META_DATA));
                的System.out.println(getActivityForApp(信息));
            }赶上(ē的NameNotFoundException){
            // TODO自动生成catch块
            e.printStackTrace();
            }
        }
    打破;
}返回结果;

}

 私有静态组件名getActivityForApp(RunningAppProcessInfo目标){
    组件名的结果= NULL;
    ActivityManager.RunningTaskInfo信息;    如果(目标== NULL)
        返回null;    如果(上午== NULL)
        AM =(ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
    清单&LT; ActivityManager.RunningTaskInfo&GT; L = am.getRunningTasks(9999);
    迭代器&LT; ActivityManager.RunningTaskInfo&GT; I = l.iterator();    而(i.hasNext()){
        信息= i.next();
        如果(info.baseActivity.getPackageName()。等于(target.processName)){
            结果= info.topActivity;
            打破;
        }
    }
    返回结果;
}


解决方案

找到了答案,我的继承人是如何做的。我只是从一个后台服务呼叫getForegroundApp()。

 公共字符串getForegroundApp()抛出{的NameNotFoundException        RunningTaskInfo资讯= NULL;
        ActivityManager上午;
        AM =(ActivityManager)mContext.getSystemService(ACTIVITY_SERVICE);
        清单&LT; RunningTaskInfo&GT; L = am.getRunningTasks(1000);
        的System.out.println(升);
        迭代器&LT; RunningTaskInfo&GT; I = l.iterator();
        字符串PACKNAME =新的String();
        字符串的appName =新的String();
        ApplicationInfo APPINFO =新ApplicationInfo();        而(i.hasNext()){
            信息= i.next();
            PACKNAME = info.topActivity.getPackageName();
            如果(!packName.equals(com.htc.launcher)及和放大器;!packName.equals(com.android.launcher)){//这可能是什么原因造成的问题。不确定。
                PACKNAME = info.topActivity.getPackageName();
                打破;
            }
            信息= i.next();
            PACKNAME = info.topActivity.getPackageName();
            打破;
            }
        返回PACKNAME;
        }

我知道这是不是最好的方法,我尤其是如何我确定该发射器没有打开,因为有更多的发射在那里的不仅仅是这两个我列出。但是,这适合我的使用案例,所以我想我会分享。

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

解决方案

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.

这篇关于Android的后台服务,以确定前台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆