【【Android|Android 获取本地已安装的应用程序】有一个需求获取本地指定的系统App和已经下载的App列表:
实现非常简单,首先获取本机已安装的App,然后把我们指定的系统App过滤掉,存入一个List集合中,然后把其他的系统App过滤掉,剩下的在放入,另外一个List集合中,组合就Ok。
贴下主要代码:
/**
* Created by Xiho on 12:10
* MyBlog: xuhao.tech
* GitHub: github.com/git-xuhao
* Function:
*/
public class AppUtils {public static final String PACKAGE_OTA="com.sunmi.ota";
public static final String PACKAGE_MARKET="woyou.market";
public static final String PACKAGE_HARD_WARE_KEEPER="com.woyou.hardwarekeeper";
public static final String PACKAGE_UDH="com.woyou.udh";
public static final String PACKAGE_SETTING="com.android.settings";
/**
* 过滤自定义的App和已下载的App
* @param packageManager
* @return
*/
public static List scanInstallApp(PackageManager packageManager,int filterType) {
List myAppInfos = new ArrayList();
List mFilterApps = new ArrayList();
try {
List packageInfos = packageManager.getInstalledPackages(0);
for (int i = 0;
i < packageInfos.size();
i++) {
PackageInfo packageInfo = packageInfos.get(i);
//过滤指定的app
String tempPackageName=packageInfo.packageName;
if(tempPackageName.equals(PACKAGE_OTA)||tempPackageName.equals(PACKAGE_MARKET)||tempPackageName.equals(PACKAGE_HARD_WARE_KEEPER)
||tempPackageName.equals(PACKAGE_UDH)||tempPackageName.equals(PACKAGE_SETTING)){
AppInfo appInfo = new AppInfo();
appInfo.setAppName((String) packageInfo.applicationInfo.loadLabel(packageManager));
if (packageInfo.applicationInfo.loadIcon(packageManager) == null) {
continue;
}
appInfo.setAppIcon(packageInfo.applicationInfo.loadIcon(packageManager));
mFilterApps.add(appInfo);
continue;
}//过滤掉系统app
if ((ApplicationInfo.FLAG_SYSTEM & packageInfo.applicationInfo.flags) != 0) {
continue;
}AppInfo appInfo = new AppInfo();
appInfo.setAppName((String) packageInfo.applicationInfo.loadLabel(packageManager));
if (packageInfo.applicationInfo.loadIcon(packageManager) == null) {
continue;
}
appInfo.setAppIcon(packageInfo.applicationInfo.loadIcon(packageManager));
myAppInfos.add(appInfo);
}
myAppInfos.addAll(mFilterApps);
} catch (Exception e) {
e.printStackTrace();
}
return myAppInfos;
}
由于内容很简单,就不上其他源码了。
推荐阅读
- 【Android|Android ViewPager的简单使用
- 【Android|Android Fragment 真正的完全解析(上)
- 【android|Android Https相关完全解析 当OkHttp遇到Https
- 【android|Android OkHttp完全解析 是时候来了解OkHttp了
- 【Android|Android NestedScrolling机制完全解析 带你玩转嵌套滑动
- 【Android|Android UI - 实现广告Banner轮播效果