[英]Android background service to determine foreground application
我正在開發一個應用程序,它將全天監控電話使用情況。 為此,我有一個后台服務,它在設備啟動時啟動並不斷輪詢以找出當前的前台應用程序是什么。 當我只是單擊一個應用程序然后退出並單擊另一個應用程序時,以下代碼有效。 現在假設我打開瀏覽器並轉到不同的網頁,它將停止將瀏覽器注冊為前台應用程序。 或者例如,我打開google talk,它會將其注冊為當前活動,但是當我發送消息時,它將不再將其注冊為前台應用程序。 關於這個錯誤可能是什么原因的任何想法或想法? 如果需要,我會提供任何其他信息,請告訴我。 也許答案就在這篇文章中,我就是找不到…… 從后台任務或服務中確定當前的前台應用程序
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;
}
下面的代碼對我有用。 列表中的第一個任務是我需要確定前台應用程序名稱。
ActivityManager activityManager = (ActivityManager) getSystemService(UpdateService.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> appProcesses = activityManager.getRunningTasks(1);
Log.i("MyApp", "Foreground Package: " + appProcesses.get(0).topActivity.getPackageName());
找到了答案,繼承人我是如何做到的。 我只是從后台服務調用 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;
}
我知道這不是最好的方法,尤其是我如何確定啟動器未打開,因為除了我列出的兩個之外,還有更多的啟動器。 但這適用於我的用例,所以我想我會分享。
這是一個舊線程,但我只想在這里添加一條注釋,即 API 級別 21 中不推薦使用 getRunningsTasks(),因此上述使用 getRunningsTasks() 的解決方案將不起作用。 看
https://developer.android.com/reference/android/app/ActivityManager
getRunningTasks(int maxNum) - 此方法在 API 級別 21 中已棄用。自 Build.VERSION_CODES.LOLLIPOP 起,此方法不再適用於第三方應用程序:引入以文檔為中心的最近記錄意味着它可能會將人員信息泄露給調用者. 為了向后兼容,它仍將返回其數據的一小部分:至少是調用者自己的任務,可能還有一些其他任務,例如已知不敏感的 home。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.