用于确定前台应用程序的Android后台服务

问题描述 投票:3回答:3

我正在开发一个可全天监控电话使用情况的应用程序。为此,我有一个后台服务,该服务在设备启动时启动,并不断进行轮询以找出当前的前台应用程序是什么。当我只是单击一个应用程序然后退出该应用程序并单击另一个应用程序时,以下代码将起作用。现在,假设我打开浏览器并转到另一个网页,它将停止将浏览器注册为前台应用程序。或者例如,我打开google talk,它将把它注册为当前活动,但是当我发送消息时,它将不再将其注册为前台应用程序。关于此错误可能是什么原因的任何想法或想法?如果需要,我会提供其他信息,请让我知道。也许答案就在这篇文章中,但我找不到它... 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;
}
android service foreground
3个回答
5
投票

下面的代码对我有用。列表中的第一个任务是确定前台应用程序名称所需的内容。

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

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

1
投票

找到答案,这是我的方法。我只是从后台服务中调用getForegroundApp()。

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

我知道这不是最好的方法,特别是我确定启动器未打开的方式,因为那里的启动器比我列出的两个还要多。但这适用于我的用例,所以我想我会分享。


0
投票

这是一个旧线程,但我只想在此处添加一条注释,即在API级别21中弃用了getRunningsTasks(),因此,上面使用getRunningsTasks()的解决方案将不起作用。参见

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

getRunningTasks(int maxNum)-此方法在API级别21中已弃用。从Build.VERSION_CODES.LOLLIPOP开始,此方法不再对第三方应用程序可用:以文档为中心的最新消息的引入意味着它可以将个人信息泄露给调用者。为了向后兼容,它仍将返回其数据的一小部分:至少调用者自己的任务,以及可能已知的一些其他不敏感的任务,例如home。

© www.soinside.com 2019 - 2024. All rights reserved.