1、曾经遇到过一个需求,获取手机已安装的播放软件信息,整理下代码如下:
package cn.geekapp.util;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
public class PackageUtil {
private static PackageUtil instance_ = null;
private static Context context_ = null;
private PackageUtil() {
}
public static PackageUtil getInstance(Context context){
if(instance_ == null){
instance_ = new PackageUtil();
}
context_ = context;
return instance_;
}
public List getVideoInfos(){
PackageManager pm = context_.getPackageManager();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("video/*");
List list = pm.queryIntentActivities(intent, 0);
return list;
}
public List getVideoInfos(){
PackageManager pm = context_.getPackageManager();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("video/*");
List list = pm.queryIntentActivities(intent, 0);
List infoList = new ArrayList();
for(ResolveInfo rInfo : list){
PackageUtil.InfoBean bean = new PackageUtil.InfoBean();
bean.label = (String) rInfo.loadLabel(pm);
bean.icon = rInfo.loadIcon(pm);
bean.name = rInfo.activityInfo != null ? rInfo.activityInfo.name : null;
bean.packageName = rInfo.activityInfo != null ? rInfo.activityInfo.packageName : null;
infoList.add(bean);
}
return infoList;
}
public class InfoBean{
public String label;
public Drawable icon;
public String name;
public String packageName;
}
}
2、若想获取其他软件,如安装的音乐播放器信息,修改intent.setType(“video/*”)为intent.setType(“audio/*”)即可,以此类推,就可以发出不同的意图,返回相应的处理软件信息。
3、ResolveInfo类常用方法:
public Drawable loadIcon(PackageManager pm)//获取应用图标
public CharSequence loadLabel(PackageManager pm)//获取应用名称
原创内容转载请保留出处GEEK笔记(https://www.geekapp.cn/)。