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;
}
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);
}
}
}
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); // 添加至列表中
}
}
}
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 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 >
< 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 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>