Android中获取后台正在运行的应用列表(附源码)

       在Android设备中,按Back键会将当前的Activity出栈销毁,而按HOME键却会将之隐藏到后台。如若有多个这样的程序这样操作,我们不知道后台到底有哪些正在运行的应用程序。在系统设置中一般列举的是正在运行的服务和正在运行的进程。一个任务有多个 Activity,以栈的形式存放。一个应用程序可以分布在不同的task中,不同的应用程序可以存在于相同的进程中。我认为通常我们所见的应用程序是指 Task中的Activity。Android中的进程概念比较混乱,不像Windows那样容易理解。Windows中一个应用对应一个进程,应用关闭,进程结束。但Android中应用退出后(指主Activity出栈),进程并未退出。Android代码庞大、设计复杂,需要时间不断研究,点点滴滴积累。

    

     一、在onCreate中注册一个Receiver,用以监听是否卸载了应用,从而作相应的更新操作

        mUninstallReceiver = new UninstallReceiver();
        //监听卸载广播
        IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_REMOVED);
        filter.addDataScheme("package");
        this.registerReceiver(mUninstallReceiver, filter);

    二、由于killBackgroundProcesses只能对进程进行操作,对当前Task操作无效,具体原因暂未查明。所以我先查出当前Task列表和进程列表,然后将两者比较,取进程列表中一个与Task列表包名相同的子集。代码如下:

               

        am = (ActivityManager)getSystemService(ACTIVITY_SERVICE);   
        List list = am.getRunningAppProcesses();   
        PackageManager pm = this.getApplicationContext().getPackageManager();   

        this.getRunningTasks();
        
        for (ActivityManager.RunningAppProcessInfo pi : list) {   
            if (pm.getLaunchIntentForPackage(pi.processName) != null) {   
                try {   
                    ApplicationInfo ai = pm.getApplicationInfo(pi.processName,PackageManager.GET_META_DATA);   
                    HashMap map = new HashMap();
                    String packageName = pi.processName;
                    Drawable dw = ai.loadIcon(pm);
                    map.put("AppIcon",dw);
                    map.put("AppLabel",ai.loadLabel(pm));
                    map.put("AppPackageName",pi.processName);
                    if(!isSystemApplication(this,packageName)&&!isSelf(packageName)&&!isLauncher(packageName)&&isRunningApp(packageName))
                    {
                        listItem.add(map);
                    }
                } catch (NameNotFoundException e) {   
                    e.printStackTrace();   
                }
            }   
        }

   三、判断是否为Launcher,一般不需要将Launcher列举出来

   

     private boolean isLauncher(String packageName) {
        PackageManager pm = this.getPackageManager();
        Intent it = new Intent(Intent.ACTION_MAIN);
        it.addCategory(Intent.CATEGORY_HOME);
        
        List riList =pm.queryIntentActivities(it,0);
        
        for (ResolveInfo ri : riList) {
               if(ri.activityInfo.packageName.equals(packageName)){    
               return true;
              }
        }

        return false;
    }

   四、长换列表ITEM响应如下:
 
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        Toast.makeText(ProcessManager.this,item.getTitle(),Toast.LENGTH_SHORT).show();
        AdapterView.AdapterContextMenuInfo menuInfo =(AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        PackageManager pm = this.getPackageManager();
        Intent intent;
        switch (item.getItemId()) {
        case 0:
        //终止程序
        Log.e(TAG,listItem.get(menuInfo.position).get("AppPackageName").toString());
        am.killBackgroundProcesses(listItem.get(menuInfo.position).get("AppPackageName").toString());                    
        listItem.remove(menuInfo.position);
        listItemAdapter.notifyDataSetChanged();
        break;
        case 1:
        //卸载程序
            curPosition = menuInfo.position;
            Uri uri = Uri.fromParts("package",listItem.get(menuInfo.position).get("AppPackageName").toString(),null);
            Intent it = new Intent(Intent.ACTION_DELETE,uri);
            this.startActivity(it);
        break;
        case 2:
        //切换程序
        intent = pm.getLaunchIntentForPackage(listItem.get(menuInfo.position).get("AppPackageName").toString());
        this.startActivity(intent);
        break;
        default:
        break;
        };
        
        return super.onContextItemSelected(item);
    }

      以上只是对程序结构作大致介绍,欲作详细了解,请下载源码参考,并多多指教:

         点击打开链接



你可能感兴趣的:(Android)