过滤手机安装的应用程序

package com.qiner.appinfo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.qiner.appinfo.R;

import android.app.Activity;
import android.app.Application;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity  {

 public static final int FILTER_ALL_APP = 0; // 所有应用程序
 public static final int FILTER_SYSTEM_APP = 1; // 系统程序
 public static final int FILTER_THIRD_APP = 2; // 第三方应用程序
 public static final int FILTER_SDCARD_APP = 3; // 安装在SDCard的应用程序

 private ListView listview = null;

 private PackageManager pm;
 private int filter = FILTER_ALL_APP;
 private List<AppInfo> mlistAppInfo ;
 private BrowseApplicationInfoAdapter browseAppAdapter = null ;
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.browse_app_list);
  listview = (ListView) findViewById(R.id.listviewApp);
  if(getIntent()!=null){
   filter = getIntent().getIntExtra("filter", 0) ;
  }
  mlistAppInfo = queryFilterAppInfo(filter); // 查询所有应用程序信息
  // 构建适配器,并且注册到listView
  browseAppAdapter = new BrowseApplicationInfoAdapter(this, mlistAppInfo);
  listview.setAdapter(browseAppAdapter);
 }
 // 根据查询条件,查询特定的ApplicationInfo
 private List<AppInfo> queryFilterAppInfo(int filter) {
  pm = this.getPackageManager();
  // 查询所有已经安装的应用程序
  List<ApplicationInfo> listAppcations = pm
    .getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
  Collections.sort(listAppcations,
    new ApplicationInfo.DisplayNameComparator(pm));// 排序
  List<AppInfo> appInfos = new ArrayList<AppInfo>(); // 保存过滤查到的AppInfo
  // 根据条件来过滤
  switch (filter) {
  case FILTER_ALL_APP: // 所有应用程序
   appInfos.clear();
   for (ApplicationInfo app : listAppcations) {
    appInfos.add(getAppInfo(app));
   }
   return appInfos;
  case FILTER_SYSTEM_APP: // 系统程序
   appInfos.clear();
   for (ApplicationInfo app : listAppcations) {
    if ((app.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
     appInfos.add(getAppInfo(app));
    }
   }
   return appInfos;
  case FILTER_THIRD_APP: // 第三方应用程序
   appInfos.clear();
   for (ApplicationInfo app : listAppcations) {
    if ((app.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) {
     appInfos.add(getAppInfo(app));
    }
   }
   break;
  case FILTER_SDCARD_APP: // 安装在SDCard的应用程序
   appInfos.clear();
   for (ApplicationInfo app : listAppcations) {
    if ((app.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
     appInfos.add(getAppInfo(app));
    }
   }
   return appInfos;
  default:
   return null;
  }
  return appInfos;
 }
 // 构造一个AppInfo对象 ,并赋值
 private AppInfo getAppInfo(ApplicationInfo app) {
  AppInfo appInfo = new AppInfo();
  appInfo.setAppLabel((String) app.loadLabel(pm));
  appInfo.setAppIcon(app.loadIcon(pm));
  appInfo.setPkgName(app.packageName);
  return appInfo;
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值