Android之使用PackageManager取得程序的包名、图标

本文介绍了一个Android应用,能够获取并展示设备上安装的所有应用程序的信息,包括应用名称、图标及包名等。通过使用PackageManager,实现了从系统中检索到每个应用程序的基本信息,并利用自定义Adapter将这些信息展示在一个ListView中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android之使用PackageManager取得程序的包名、图标等

效果图:

Model代码:

复制代码
public classAppInfo{

privateStringappLabel;
privateDrawableappIcon;
privateIntentintent;
privateStringpkgName;

publicAppInfo(){}

publicStringgetAppLabel(){
returnappLabel;
}
public voidsetAppLabel(StringappName){
this.appLabel=appName;
}
publicDrawablegetAppIcon(){
returnappIcon;
}
public voidsetAppIcon(DrawableappIcon){
this.appIcon=appIcon;
}
publicIntentgetIntent(){
returnintent;
}
public voidsetIntent(Intentintent){
this.intent=intent;
}
publicStringgetPkgName(){
returnpkgName;
}
public voidsetPkgName(StringpkgName){
this.pkgName=pkgName;
}

}

复制代码

继承BaseAdapter:

复制代码
public classBrowseApplicationInfoAdapter extendsBaseAdapter{

privateList<AppInfo>mlistAppInfo= null;

LayoutInflaterinfater= null;

publicBrowseApplicationInfoAdapter(Contextcontext,List<AppInfo>apps){
infater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mlistAppInfo=apps;
}
@Override
public intgetCount(){
// TODOAuto-generatedmethodstub
System.out.println("size"+mlistAppInfo.size());
returnmlistAppInfo.size();
}
@Override
publicObjectgetItem( intposition){
// TODOAuto-generatedmethodstub
returnmlistAppInfo.get(position);
}
@Override
public longgetItemId( intposition){
// TODOAuto-generatedmethodstub
return0;
}
@Override
publicViewgetView( intposition,Viewconvertview,ViewGrouparg2){
System.out.println("getViewat"+position);
Viewview= null;
ViewHolderholder= null;
if(convertview== null||convertview.getTag()== null){
view=infater.inflate(R.layout.browse_app_item, null);
holder= newViewHolder(view);
view.setTag(holder);
}
else{
view=convertview;
holder=(ViewHolder)convertview.getTag();
}
AppInfoappInfo=(AppInfo)getItem(position);
holder.appIcon.setImageDrawable(appInfo.getAppIcon());
holder.tvAppLabel.setText(appInfo.getAppLabel());
holder.tvPkgName.setText(appInfo.getPkgName());
returnview;
}

classViewHolder{
ImageViewappIcon;
TextViewtvAppLabel;
TextViewtvPkgName;

publicViewHolder(Viewview){
this.appIcon=(ImageView)view.findViewById(R.id.imgApp);
this.tvAppLabel=(TextView)view.findViewById(R.id.tvAppLabel);
this.tvPkgName=(TextView)view.findViewById(R.id.tvPkgName);
}
}
}
复制代码

MainActivity:

复制代码
public class MainActivity extends Activity implements OnItemClickListener{

private ListViewlistview= null ;

private List<AppInfo>mlistAppInfo= null ;

@Override
public void onCreate(BundlesavedInstanceState){
super .onCreate(savedInstanceState);
setContentView(R.layout.browse_app_list);

listview=(ListView)findViewById(R.id.listviewApp);
mlistAppInfo= new ArrayList<AppInfo>();
queryAppInfo(); // 查询所有应用程序信息
BrowseApplicationInfoAdapterbrowseAppAdapter= new BrowseApplicationInfoAdapter(
this ,mlistAppInfo);
listview.setAdapter(browseAppAdapter);
listview.setOnItemClickListener( this );
}
// 点击跳转至该应用程序
public void onItemClick(AdapterView<?>arg0,Viewview, int position,
long arg3){
// TODOAuto-generatedmethodstub
Intentintent=mlistAppInfo.get(position).getIntent();
startActivity(intent);
}
// 获得所有启动Activity的信息,类似于Launch界面
public void queryAppInfo(){
PackageManagerpm= this .getPackageManager(); // 获得PackageManager对象
IntentmainIntent= new Intent(Intent.ACTION_MAIN, null );
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
// 通过查询,获得所有ResolveInfo对象.
List<ResolveInfo>resolveInfos=pm
.queryIntentActivities(mainIntent,PackageManager.MATCH_DEFAULT_ONLY);
// 调用系统排序,根据name排序
// 该排序很重要,否则只能显示系统应用,而不能列出第三方应用程序
Collections.sort(resolveInfos, new ResolveInfo.DisplayNameComparator(pm));
if (mlistAppInfo!= null ){
mlistAppInfo.clear();
for (ResolveInforeInfo:resolveInfos){
StringactivityName=reInfo.activityInfo.name;//获得该应用程序的启动Activity的name
StringpkgName=reInfo.activityInfo.packageName;//获得应用程序的包名
StringappLabel=(String)reInfo.loadLabel(pm);//获得应用程序的Label
Drawableicon=reInfo.loadIcon(pm);//
获得应用程序图标
// 为应用程序的启动Activity准备Intent
IntentlaunchIntent= new Intent();
launchIntent.setComponent( new ComponentName(pkgName,
activityName));
// 创建一个AppInfo对象,并赋值
AppInfoappInfo= new AppInfo();
appInfo.setAppLabel(appLabel);
appInfo.setPkgName(pkgName);
appInfo.setAppIcon(icon);
appInfo.setIntent(launchIntent);
mlistAppInfo.add(appInfo); // 添加至列表中
}
}
}

}

复制代码


AndroidManifest文件:

复制代码
<? xmlversion="1.0"encoding="utf-8" ?>
< manifest xmlns:android ="http://schemas.android.com/apk/res/android"
package
="com.qin.appinfo" android:versionCode ="1" android:versionName ="1.0" >
< application android:icon ="@drawable/icon" android:label ="@string/app_name" >
< activity android:name =".MainActivity" android:label ="@string/app_name" >
< intent-filter >
< action android:name ="android.intent.action.MAIN" />
< category android:name ="android.intent.category.LAUNCHER" />
< category android:name ="android.intent.category.DEFAULT" />
</ intent-filter >
</ activity >
</ application >

</manifest>

复制代码


browse_app_item.xml:

复制代码
<? xmlversion="1.0"encoding="utf-8" ?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width
="fill_parent" android:layout_height ="50dip" >

< ImageView android:id ="@+id/imgApp" android:layout_width ="wrap_content"
android:layout_height
="fill_parent" ></ ImageView >
< RelativeLayout android:layout_width ="fill_parent" android:layout_marginLeft ="10dip"
android:layout_height
="40dip" >
< TextView android:id ="@+id/tvLabel" android:layout_width ="wrap_content"
android:layout_height
="wrap_content" android:text ="AppLable:" ></ TextView >
< TextView android:id ="@+id/tvAppLabel" android:layout_width ="wrap_content"
android:layout_toRightOf
="@id/tvLabel" android:layout_height ="wrap_content"
android:layout_marginLeft
="3dip" android:text ="Label" android:textColor ="#FFD700" ></ TextView >
< TextView android:id ="@+id/tvName" android:layout_width ="wrap_content"
android:layout_height
="wrap_content" android:layout_below ="@id/tvLabel"
android:text
="包名:" ></ TextView >
< TextView android:id ="@+id/tvPkgName" android:layout_width ="wrap_content"
android:layout_height
="wrap_content" android:layout_below ="@id/tvAppLabel"
android:layout_alignLeft
="@id/tvAppLabel" android:textColor ="#FFD700" ></ TextView >
</ RelativeLayout >
</ LinearLayout >
复制代码

broswe_app_list.xml:

复制代码
<? xmlversion="1.0"encoding="utf-8" ?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation
="vertical" android:layout_width ="fill_parent"
android:layout_height
="fill_parent" >>
< ListView android:id ="@+id/listviewApp" android:layout_width ="fill_parent"
android:layout_height
="fill_parent" ></ ListView >

</LinearLayout>

复制代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值