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

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

         点击打开链接



更多相关文章

  1. android小程序--猜猜红桃A在哪儿
  2. 为Android的apk应用程序文件加壳以防止反编译的教程
  3. 6 个可以让代码变得更整洁的 Android(安卓)库
  4. Android跨程序共享数据
  5. android动态加载class文件(DexClassLoader可从SD卡加载)(android插
  6. 资源分享-良心资源,适合初,中级程序员
  7. Android程序猿:“我有五年开发经验”“不,你只是把一年的工作经验
  8. Android年终盘点:阿里程序员必备技能知识点,程序员必须收藏
  9. Android开发之程序员必须要懂得Android的重要设计理念

随机推荐

  1. Android(安卓)Layout 属性大全
  2. ContentProvider数据共享
  3. Android(安卓)横屏不重启 Activity
  4. android布局属性详解
  5. Android(安卓)系统自带图标
  6. RelativeLayout属性详解
  7. Android(安卓)Drawable在XML和JAVA的表示
  8. Android(安卓)# 基础汇总
  9. android shape
  10. android相对布局