Retrieve package name of applications whom are cur

2019-07-25 04:09发布

Can someone help me with sample code on retrieving the packages names of the activities that are currently running in Android?

Edited:

Code 1

    ActivityManager am = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> processes = am.getRunningAppProcesses();
    if (processes != null){
        for (int i=0; i<processes.size(); i++){
            RunningAppProcessInfo temp = processes.get(i);
            String pName = temp.processName;
            for (int k=0; k<blacklist.size(); k++){
                if (pName.equalsIgnoreCase((String)blacklist.get(k))){
                    int pid = android.os.Process.getUidForName(pName);
                    android.os.Process.killProcess(pid);
                }
            }
     }

Code 2?

  int pid = temp.pid;
    String[] packages = temp.pkgList;
            for (int j=0; j<packages.length; j++){
                String packageName = packages[j];
                for (int k=0; k<blacklist.size(); k++){
                    if (packageName.equalsIgnoreCase((String)blacklist.get(k))){
                        //Android 2.2+ kill all background processes
                        am.killBackgroundProcesses(packageName);
                    }
                }
            }

Assuming that black has the application package name of program which I have blacklisted and want to terminate. Which of the above code is the correct way in which to implement my task killer. Can someone explain the difference between killBackgroundProcesses(packageName) and android.os.Process.killProcess(pid) as I cant seem to get the difference between them.

3条回答
倾城 Initia
2楼-- · 2019-07-25 04:25
        ActivityManager am =(ActivityManager)context.getSystemService(ACTIVITY_SERVICE);
  List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
  ActivityManager.RunningTaskInfo task = tasks.get(0); // current task
   ComponentName rootActivity = task.baseActivity;


      rootActivity.getPackageName();//*currently active applications package name*

using this in a loop,you will get package name of all currently running app ,to get running app name, see this EXAMPLE

查看更多
虎瘦雄心在
3楼-- · 2019-07-25 04:34

first of you API greater then 20 then use the bellow code to definitely work. I use this code and work properly. Try it

String currentApp=null;
    UsageStatsManager usm= (UsageStatsManager) this.getSystemService(Context.USAGE_STATS_SERVICE);
    long time=System.currentTimeMillis();
    List<UsageStats> applist=usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,time - 1000*1000,time);
    if(applist !=null && applist.size() > 0)
    {
        SortedMap<Long,UsageStats> mySortedMap=new TreeMap<Long,UsageStats>();
        for (UsageStats usageStats : applist)
        {
            mySortedMap.put(usageStats.getLastTimeUsed(),usageStats);

        }
        if(mySortedMap != null && !mySortedMap.isEmpty())
        {
            currentApp=mySortedMap.get(mySortedMap.lastKey()).getPackageName();
        }
    }
    Log.e(TAG, "Current App in foreground is: " + currentApp);

    return currentApp;
查看更多
Luminary・发光体
4楼-- · 2019-07-25 04:41

You can use the getRunningAppProcesses on the Android ActivityManager to retrieve a list of running processes.

Check this link : http://developer.android.com/reference/android/app/ActivityManager.html#getRunningAppProcesses%28%29

You can use the getInstalledPackages method on the Android PackageManager to retrieve a list of all the installed apps

Check this link : http://developer.android.com/reference/android/content/pm/PackageManager.html#getInstalledPackages%28int%29

查看更多
登录 后发表回答