Android获取手机已安装APP(系统/非系统)
效果图
主体代码
private ListView mlistview;
private ListpackageInfoList;
private ListapplicationInfoList;
private MyAdapter myAdapter;
private PackageManager pm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
mlistview=findViewById(R.id.mlistview);
getAllAppNames();
myAdapter=new MyAdapter(applicationInfoList);
mlistview.setAdapter(myAdapter);
myAdapter.notifyDataSetChanged();
mlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
doStartApplicationWithPackageName(applicationInfoList.get(position).getPackageName());
}
});
}
//自定义适配器
public class MyAdapter extends BaseAdapter{
private Listlist;
public MyAdapter(Listlist){
this.list=list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=null;
if(convertView==null){
view=LayoutInflater.from(Main3Activity.this).inflate(R.layout.mlistview_layout,null);
}else{
view=convertView;
}
TextView tv_packagename=view.findViewById(R.id.tv_packagename);
TextView tv_appname=view.findViewById(R.id.tv_appname);
ImageView img=view.findViewById(R.id.img);
tv_appname.setText(list.get(position).getName());
tv_packagename.setText(list.get(position).getPackageName());
img.setImageDrawable(list.get(position).getIcon());
return view;
}
}
public void getAllAppNames(){
pm=getPackageManager();
获取到所有安装了的应用程序的信息,包括那些卸载了的,但没有清除数据的应用程序
// packageInfoList=pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
applicationInfoList=new ArrayList<>();
Listlist = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
Collections.sort(list, new ApplicationInfo.DisplayNameComparator(pm));// 排序
Log.e("-------",list.size()+"");
applicationInfoList.clear();
for (int i=0;iresolveinfoList = getPackageManager()
.queryIntentActivities(resolveIntent, 0);
if(resolveinfoList.size() == 0){
return;
}
ResolveInfo resolveinfo = resolveinfoList.iterator().next();
if (resolveinfo != null) {
String packageName = resolveinfo.activityInfo.packageName;
//App启动的Activity
String className = resolveinfo.activityInfo.name;
// LAUNCHER Intent
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
// 设置ComponentName参数1:packagename参数2:启动的Activity
ComponentName cn = new ComponentName(packageName, className);
intent.setComponent(cn);
startActivity(intent);
}
}
// 构造一个AppInfo对象 ,并赋值
private AppInfo getAppInfo(ApplicationInfo app) {
AppInfo appInfo = new AppInfo();
appInfo.setName(app.loadLabel(pm).toString());
appInfo.setIcon(app.loadIcon(pm));
appInfo.setPackageName(app.packageName);
return appInfo;
}
这里是定义的实体bean
public class AppInfo implements Serializable{
/** 序列号 */
private static final long serialVersionUID = -6660233212727684115L;
/** 名称 */
public String name;
/** (路径) */
public String path;
/** 图标 */
public Drawable icon;
/** 包名 */
public String packageName;
public static long getSerialVersionUID() {
return serialVersionUID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public Drawable getIcon() {
return icon;
}
public void setIcon(Drawable icon) {
this.icon = icon;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
}
这里是mlistview_layout.xml的布局代码
mlistview_layout.xml布局效果图
这里是activity_main3.xml布局代码
activity_main3.xml效果图