* @exception 获取所有apk
* @param Context
* @retrun List<PackageInfo> 储存所有app的列表
* **/
public List<PackageInfo> getAllApps(Context context) {
List<PackageInfo> apps = new ArrayList<PackageInfo>();
PackageManager pManager = context.getPackageManager();
// 获取手机内所有应用
List<PackageInfo> packlist = pManager.getInstalledPackages(0);
for (int i = 0; i < packlist.size(); i++) {
PackageInfo pak = (PackageInfo) packlist.get(i);
/*设置 浏览器 计算器 相机 ES文件浏览器 电视 本地影音 WPS 下载 音乐 系统apk筛选 */
if ( pak.packageName.equals("com.android.settings") ||pak.packageName.equals("com.android.browser")
||pak.packageName.equals("com.android.calculator2") ||pak.packageName.equals("com.android.camera2")
||pak.packageName.equals("com.jrm.localmm")|| pak.packageName.equals("com.estrongs.android.pop")
|| pak.packageName.equals("com.mstar.tv.tvplayer.ui")|| pak.packageName.equals("cn.wps.moffice_eng")
|| pak.packageName.equals("com.android.providers.downloads.ui")|| pak.packageName.equals("com.android.music")
|| (pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {
//pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0 非系统apk
apps.add(pak);}
// 判断是否为非系统预装的应用程序
// 这里还可以添加系统自带的,这里就先不添加了,如果有需要可以自己添加
// if()里的值如果<=0则为自己装的程序,否则为系统工程自带
// if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {
// // 添加自己已经安装的应用程序
// apps.add(pak);
// }
}
return apps;
}
/**
* @exception 获取apk的图标及改变图标
* **/
public List<AppsItemInfo> getData(){
// 获取图片、应用名、包名
pManager =this.getPackageManager();
List<PackageInfo> appList = getAllApps(this);
list = new ArrayList<AppsItemInfo>();
for (int i = 0; i < appList.size(); i++) {
PackageInfo pinfo = appList.get(i);
AppsItemInfo shareItem = new AppsItemInfo();
if (appList.get(i).packageName.equals("com.android.settings")) {
// 设置图片,根据包名替换apk图标
shareItem.setIcon(getResources().getDrawable(R.drawable.settings));
}else {
// 设置图片
shareItem.setIcon(pManager
.getApplicationIcon(pinfo.applicationInfo));
}
// 设置应用程序名字
shareItem.setLabel(pManager.getApplicationLabel(
pinfo.applicationInfo).toString());
// 设置应用程序的包名
shareItem.setPackageName(pinfo.applicationInfo.packageName);
// 设置应用程序的类名
shareItem.setClassName(pinfo.applicationInfo.className);
list.add(shareItem);
}
return list;
}
//存放apk信息的类
public class AppsItemInfo {
private Drawable icon; // 存放图片
private String label; // 存放应用程序名
private String packageName; // 存放应用程序包名
private String className; // 存放应用程序包名
public int flag = 0;
public static final int MOVE_FLAG = 1;
public static final int STATIC_FLAG = 0;
public Drawable getIcon() {
return icon;
}
public void setIcon(Drawable icon) {
this.icon = icon;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}