效果图:
Model代码:
public
class AppInfo {
private String appLabel;
private Drawable appIcon ;
private Intent intent ;
private String pkgName ;
public AppInfo(){}
public String getAppLabel() {
return appLabel;
}
public void setAppLabel(String appName) {
this.appLabel = appName;
}
public Drawable getAppIcon() {
return appIcon;
}
public void setAppIcon(Drawable appIcon) {
this.appIcon = appIcon;
}
public Intent getIntent() {
return intent;
}
public void setIntent(Intent intent) {
this.intent = intent;
}
public String getPkgName(){
return pkgName ;
}
public void setPkgName(String pkgName){
this.pkgName=pkgName ;
}
private String appLabel;
private Drawable appIcon ;
private Intent intent ;
private String pkgName ;
public AppInfo(){}
public String getAppLabel() {
return appLabel;
}
public void setAppLabel(String appName) {
this.appLabel = appName;
}
public Drawable getAppIcon() {
return appIcon;
}
public void setAppIcon(Drawable appIcon) {
this.appIcon = appIcon;
}
public Intent getIntent() {
return intent;
}
public void setIntent(Intent intent) {
this.intent = intent;
}
public String getPkgName(){
return pkgName ;
}
public void setPkgName(String pkgName){
this.pkgName=pkgName ;
}
}
继承BaseAdapter:
public
class BrowseApplicationInfoAdapter
extends BaseAdapter {
private List<AppInfo> mlistAppInfo = null;
LayoutInflater infater = null;
public BrowseApplicationInfoAdapter(Context context, List<AppInfo> apps) {
infater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mlistAppInfo = apps ;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
System.out.println("size" + mlistAppInfo.size());
return mlistAppInfo.size();
}
@Override
public Object getItem( int position) {
// TODO Auto-generated method stub
return mlistAppInfo.get(position);
}
@Override
public long getItemId( int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView( int position, View convertview, ViewGroup arg2) {
System.out.println("getView at " + position);
View view = null;
ViewHolder holder = null;
if (convertview == null || convertview.getTag() == null) {
view = infater.inflate(R.layout.browse_app_item, null);
holder = new ViewHolder(view);
view.setTag(holder);
}
else{
view = convertview ;
holder = (ViewHolder) convertview.getTag() ;
}
AppInfo appInfo = (AppInfo) getItem(position);
holder.appIcon.setImageDrawable(appInfo.getAppIcon());
holder.tvAppLabel.setText(appInfo.getAppLabel());
holder.tvPkgName.setText(appInfo.getPkgName());
return view;
}
class ViewHolder {
ImageView appIcon;
TextView tvAppLabel;
TextView tvPkgName;
public ViewHolder(View view) {
this.appIcon = (ImageView) view.findViewById(R.id.imgApp);
this.tvAppLabel = (TextView) view.findViewById(R.id.tvAppLabel);
this.tvPkgName = (TextView) view.findViewById(R.id.tvPkgName);
}
}
}
private List<AppInfo> mlistAppInfo = null;
LayoutInflater infater = null;
public BrowseApplicationInfoAdapter(Context context, List<AppInfo> apps) {
infater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mlistAppInfo = apps ;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
System.out.println("size" + mlistAppInfo.size());
return mlistAppInfo.size();
}
@Override
public Object getItem( int position) {
// TODO Auto-generated method stub
return mlistAppInfo.get(position);
}
@Override
public long getItemId( int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView( int position, View convertview, ViewGroup arg2) {
System.out.println("getView at " + position);
View view = null;
ViewHolder holder = null;
if (convertview == null || convertview.getTag() == null) {
view = infater.inflate(R.layout.browse_app_item, null);
holder = new ViewHolder(view);
view.setTag(holder);
}
else{
view = convertview ;
holder = (ViewHolder) convertview.getTag() ;
}
AppInfo appInfo = (AppInfo) getItem(position);
holder.appIcon.setImageDrawable(appInfo.getAppIcon());
holder.tvAppLabel.setText(appInfo.getAppLabel());
holder.tvPkgName.setText(appInfo.getPkgName());
return view;
}
class ViewHolder {
ImageView appIcon;
TextView tvAppLabel;
TextView tvPkgName;
public ViewHolder(View view) {
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 ListView listview = null;
private List<AppInfo> mlistAppInfo = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browse_app_list);
listview = (ListView) findViewById(R.id.listviewApp);
mlistAppInfo = new ArrayList<AppInfo>();
queryAppInfo(); // 查询所有应用程序信息
BrowseApplicationInfoAdapter browseAppAdapter = new BrowseApplicationInfoAdapter(
this, mlistAppInfo);
listview.setAdapter(browseAppAdapter);
listview.setOnItemClickListener( this);
}
// 点击跳转至该应用程序
public void onItemClick(AdapterView<?> arg0, View view, int position,
long arg3) {
// TODO Auto-generated method stub
Intent intent = mlistAppInfo.get(position).getIntent();
startActivity(intent);
}
// 获得所有启动Activity的信息,类似于Launch界面
public void queryAppInfo() {
PackageManager pm = this.getPackageManager(); // 获得PackageManager对象
Intent mainIntent = 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 (ResolveInfo reInfo : resolveInfos) {
String activityName = reInfo.activityInfo.name; // 获得该应用程序的启动Activity的name
String pkgName = reInfo.activityInfo.packageName; // 获得应用程序的包名
String appLabel = (String) reInfo.loadLabel(pm); // 获得应用程序的Label
Drawable icon = reInfo.loadIcon(pm); // 获得应用程序图标
// 为应用程序的启动Activity 准备Intent
Intent launchIntent = new Intent();
launchIntent.setComponent( new ComponentName(pkgName,
activityName));
// 创建一个AppInfo对象,并赋值
AppInfo appInfo = new AppInfo();
appInfo.setAppLabel(appLabel);
appInfo.setPkgName(pkgName);
appInfo.setAppIcon(icon);
appInfo.setIntent(launchIntent);
mlistAppInfo.add(appInfo); // 添加至列表中
}
}
}
private ListView listview = null;
private List<AppInfo> mlistAppInfo = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browse_app_list);
listview = (ListView) findViewById(R.id.listviewApp);
mlistAppInfo = new ArrayList<AppInfo>();
queryAppInfo(); // 查询所有应用程序信息
BrowseApplicationInfoAdapter browseAppAdapter = new BrowseApplicationInfoAdapter(
this, mlistAppInfo);
listview.setAdapter(browseAppAdapter);
listview.setOnItemClickListener( this);
}
// 点击跳转至该应用程序
public void onItemClick(AdapterView<?> arg0, View view, int position,
long arg3) {
// TODO Auto-generated method stub
Intent intent = mlistAppInfo.get(position).getIntent();
startActivity(intent);
}
// 获得所有启动Activity的信息,类似于Launch界面
public void queryAppInfo() {
PackageManager pm = this.getPackageManager(); // 获得PackageManager对象
Intent mainIntent = 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 (ResolveInfo reInfo : resolveInfos) {
String activityName = reInfo.activityInfo.name; // 获得该应用程序的启动Activity的name
String pkgName = reInfo.activityInfo.packageName; // 获得应用程序的包名
String appLabel = (String) reInfo.loadLabel(pm); // 获得应用程序的Label
Drawable icon = reInfo.loadIcon(pm); // 获得应用程序图标
// 为应用程序的启动Activity 准备Intent
Intent launchIntent = new Intent();
launchIntent.setComponent( new ComponentName(pkgName,
activityName));
// 创建一个AppInfo对象,并赋值
AppInfo appInfo = new AppInfo();
appInfo.setAppLabel(appLabel);
appInfo.setPkgName(pkgName);
appInfo.setAppIcon(icon);
appInfo.setIntent(launchIntent);
mlistAppInfo.add(appInfo); // 添加至列表中
}
}
}
}
AndroidManifest文件:
<?
xml version="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:
<?
xml version="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:
<?
xml version="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>
根据packagename来启动程序:
final String activityName = getActivityName(packageName); if (null == activityName) { Toast.makeText(ShowAppActivity.this, "程序无法启动", Toast.LENGTH_SHORT); return; } Intent intent = new Intent(); intent.setComponent(new ComponentName(packageName, activityName)); startActivity(intent);
根据packagename来卸载程序:
Intent deleteIntent = new Intent(); deleteIntent.setAction(Intent.ACTION_DELETE); deleteIntent.setData(Uri .parse("package:" + packagename));