Android 获取所有安装应用显示在listview上
2014-05-10 00:02
405 查看
先看效果图;
详细看代码;
[java]view plaincopypackage com.taskmanage.file;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
publicclass SoftActivity extends Activity implements Runnable ,OnItemClickListener{
privatestaticfinal String SCHEME = "package";
privatestaticfinal String APP_PKG_NAME_21 = "com.android.settings.ApplicationPkgName";
privatestaticfinal String APP_PKG_NAME_22 = "pkg";
privatestaticfinal String APP_DETAILS_PACKAGE_NAME = "com.android.settings";
privatestaticfinal String APP_DETAILS_CLASS_NAME = "com.android.settings.InstalledAppDetails";
private List<Map<String, Object>> list = null;
private ListView softlist = null;
private ProgressDialog pd;
private Context mContext;
private PackageManager mPackageManager;
private List<ResolveInfo> mAllApps;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
setContentView(R.layout.software);
setTitle("文件管理器");
mContext = this;
mPackageManager = getPackageManager();
softlist = (ListView) findViewById(R.id.softlist);
pd = ProgressDialog.show(this, "请稍候..", "正在收集软件信息...", true,false);
Thread thread = new Thread(this);
thread.start();
super.onCreate(savedInstanceState);
}
/**
* 检查系统应用程序,添加到应用列表中
*/
privatevoid bindMsg(){
//应用过滤条件
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mAllApps = mPackageManager.queryIntentActivities(mainIntent, 0);
softlist.setAdapter(new MyAdapter(mContext, mAllApps));
softlist.setOnItemClickListener(this);
//按报名排序
Collections.sort(mAllApps, new ResolveInfo.DisplayNameComparator(mPackageManager));
}
@Override
publicvoid run() {
bindMsg();
handler.sendEmptyMessage(0);
}
private Handler handler = new Handler() {
publicvoid handleMessage(Message msg) {
pd.dismiss();
}
};
class MyAdapter extends BaseAdapter{
private Context context;
private List<ResolveInfo> resInfo;
private ImageView app_icon=null;
private TextView app_tilte=null,app_des=null;
private ResolveInfo res;
private LayoutInflater infater=null;
public MyAdapter(Context context, List<ResolveInfo> resInfo) {
this.context = context;
this.resInfo = resInfo;
infater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
publicint getCount() {
return resInfo.size();
}
@Override
public Object getItem(int arg0) {
return arg0;
}
@Override
publiclong getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// View view = null;
ViewHolder holder = null;
if (convertView == null || convertView.getTag() == null) {
convertView = infater.inflate(R.layout.soft_row, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
else{
// view = convertView ;
holder = (ViewHolder) convertView.getTag() ;
}
//获取应用程序包名,程序名称,程序图标
res = resInfo.get(position);
holder.appIcon.setImageDrawable(res.loadIcon(mPackageManager));
holder.tvAppLabel.setText(res.loadLabel(mPackageManager).toString());
holder.tvPkgName.setText(res.activityInfo.packageName+'\n'+res.activityInfo.name);
return convertView;
/*convertView = LayoutInflater.from(context).inflate(R.layout.soft_row, null);
app_icon = (ImageView)convertView.findViewById(R.id.img);
app_tilte = (TextView)convertView.findViewById(R.id.name);
app_des = (TextView)convertView.findViewById(R.id.desc);
res = resInfo.get(position);
app_icon.setImageDrawable(res.loadIcon(mPackageManager));
app_tilte.setText(res.loadLabel(mPackageManager).toString());
app_des.setText(res.activityInfo.packageName+'\n'+res.activityInfo.name);
return convertView;*/
}
}
//设定界面布局
class ViewHolder {
ImageView appIcon;
TextView tvAppLabel;
TextView tvPkgName;
public ViewHolder(View view) {
this.appIcon = (ImageView) view.findViewById(R.id.img);
this.tvAppLabel = (TextView) view.findViewById(R.id.name);
this.tvPkgName = (TextView) view.findViewById(R.id.desc);
}
}
/**
* 单击应用程序后进入系统应用管理界面
*/
@Override
publicvoid onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
ResolveInfo res = mAllApps.get(position);
String pkg=res.activityInfo.packageName;
Intent intent = new Intent();
finalint apiLevel = Build.VERSION.SDK_INT;
if (apiLevel >= 9) {//2.2版本后
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts(SCHEME, pkg, null);
intent.setData(uri);
}else{//2.2之前
final String appPkgName = (apiLevel == 8 ? APP_PKG_NAME_22
: APP_PKG_NAME_21);
intent.setAction(Intent.ACTION_VIEW);
intent.setClassName(APP_DETAILS_PACKAGE_NAME,
APP_DETAILS_CLASS_NAME);
intent.putExtra(appPkgName, pkg);
}
startActivity(intent);
}
}
software.xml布局;
[java]view plaincopy<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:id="@+id/softlist" />
</LinearLayout>
soft_row.xml布局;
[java]view plaincopy<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/vw1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView android:id="@+id/img"
android:layout_width="32dip"
android:layout_margin="4dip"
android:layout_height="32dip"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="@+id/name"
android:textSize="18sp"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/desc"
android:textSize="14sp"
android:layout_width="fill_parent"
android:paddingLeft="10dip"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
最后别忘了加权限;
[html]view plaincopy<uses-permissionandroid:name="android.permission.READ_PHONE_STATE"/>
<uses-permissionandroid:name="android.permission.GET_TASKS"/>
【转】http://blog.csdn.net/yudajun/article/details/7956933
详细看代码;
[java]view plaincopypackage com.taskmanage.file;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
publicclass SoftActivity extends Activity implements Runnable ,OnItemClickListener{
privatestaticfinal String SCHEME = "package";
privatestaticfinal String APP_PKG_NAME_21 = "com.android.settings.ApplicationPkgName";
privatestaticfinal String APP_PKG_NAME_22 = "pkg";
privatestaticfinal String APP_DETAILS_PACKAGE_NAME = "com.android.settings";
privatestaticfinal String APP_DETAILS_CLASS_NAME = "com.android.settings.InstalledAppDetails";
private List<Map<String, Object>> list = null;
private ListView softlist = null;
private ProgressDialog pd;
private Context mContext;
private PackageManager mPackageManager;
private List<ResolveInfo> mAllApps;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
setContentView(R.layout.software);
setTitle("文件管理器");
mContext = this;
mPackageManager = getPackageManager();
softlist = (ListView) findViewById(R.id.softlist);
pd = ProgressDialog.show(this, "请稍候..", "正在收集软件信息...", true,false);
Thread thread = new Thread(this);
thread.start();
super.onCreate(savedInstanceState);
}
/**
* 检查系统应用程序,添加到应用列表中
*/
privatevoid bindMsg(){
//应用过滤条件
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mAllApps = mPackageManager.queryIntentActivities(mainIntent, 0);
softlist.setAdapter(new MyAdapter(mContext, mAllApps));
softlist.setOnItemClickListener(this);
//按报名排序
Collections.sort(mAllApps, new ResolveInfo.DisplayNameComparator(mPackageManager));
}
@Override
publicvoid run() {
bindMsg();
handler.sendEmptyMessage(0);
}
private Handler handler = new Handler() {
publicvoid handleMessage(Message msg) {
pd.dismiss();
}
};
class MyAdapter extends BaseAdapter{
private Context context;
private List<ResolveInfo> resInfo;
private ImageView app_icon=null;
private TextView app_tilte=null,app_des=null;
private ResolveInfo res;
private LayoutInflater infater=null;
public MyAdapter(Context context, List<ResolveInfo> resInfo) {
this.context = context;
this.resInfo = resInfo;
infater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
publicint getCount() {
return resInfo.size();
}
@Override
public Object getItem(int arg0) {
return arg0;
}
@Override
publiclong getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// View view = null;
ViewHolder holder = null;
if (convertView == null || convertView.getTag() == null) {
convertView = infater.inflate(R.layout.soft_row, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
else{
// view = convertView ;
holder = (ViewHolder) convertView.getTag() ;
}
//获取应用程序包名,程序名称,程序图标
res = resInfo.get(position);
holder.appIcon.setImageDrawable(res.loadIcon(mPackageManager));
holder.tvAppLabel.setText(res.loadLabel(mPackageManager).toString());
holder.tvPkgName.setText(res.activityInfo.packageName+'\n'+res.activityInfo.name);
return convertView;
/*convertView = LayoutInflater.from(context).inflate(R.layout.soft_row, null);
app_icon = (ImageView)convertView.findViewById(R.id.img);
app_tilte = (TextView)convertView.findViewById(R.id.name);
app_des = (TextView)convertView.findViewById(R.id.desc);
res = resInfo.get(position);
app_icon.setImageDrawable(res.loadIcon(mPackageManager));
app_tilte.setText(res.loadLabel(mPackageManager).toString());
app_des.setText(res.activityInfo.packageName+'\n'+res.activityInfo.name);
return convertView;*/
}
}
//设定界面布局
class ViewHolder {
ImageView appIcon;
TextView tvAppLabel;
TextView tvPkgName;
public ViewHolder(View view) {
this.appIcon = (ImageView) view.findViewById(R.id.img);
this.tvAppLabel = (TextView) view.findViewById(R.id.name);
this.tvPkgName = (TextView) view.findViewById(R.id.desc);
}
}
/**
* 单击应用程序后进入系统应用管理界面
*/
@Override
publicvoid onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
ResolveInfo res = mAllApps.get(position);
String pkg=res.activityInfo.packageName;
Intent intent = new Intent();
finalint apiLevel = Build.VERSION.SDK_INT;
if (apiLevel >= 9) {//2.2版本后
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts(SCHEME, pkg, null);
intent.setData(uri);
}else{//2.2之前
final String appPkgName = (apiLevel == 8 ? APP_PKG_NAME_22
: APP_PKG_NAME_21);
intent.setAction(Intent.ACTION_VIEW);
intent.setClassName(APP_DETAILS_PACKAGE_NAME,
APP_DETAILS_CLASS_NAME);
intent.putExtra(appPkgName, pkg);
}
startActivity(intent);
}
}
software.xml布局;
[java]view plaincopy<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:id="@+id/softlist" />
</LinearLayout>
soft_row.xml布局;
[java]view plaincopy<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/vw1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView android:id="@+id/img"
android:layout_width="32dip"
android:layout_margin="4dip"
android:layout_height="32dip"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="@+id/name"
android:textSize="18sp"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/desc"
android:textSize="14sp"
android:layout_width="fill_parent"
android:paddingLeft="10dip"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
最后别忘了加权限;
[html]view plaincopy<uses-permissionandroid:name="android.permission.READ_PHONE_STATE"/>
<uses-permissionandroid:name="android.permission.GET_TASKS"/>
【转】http://blog.csdn.net/yudajun/article/details/7956933
相关文章推荐
- Android初学------系统设置之获取所有安装应用大小
- 如何获取所有安装的Android应用并且可以运行其中一个?
- [Android Pro] 获取手机已经安装的应用 和 获取当前正在运行的所有进程(一个uid对应多个pid)
- Android自定义Dialog布局listview 实现显示安装的应用
- android获取所有应用信息,判断系统应用或第三方应用,监听应用安装和卸载
- [Android Pro] 获取手机已经安装的应用 和 获取当前正在运行的所有进程(一个uid对应多个pid)
- 【转】android获取所有安装的非系统应用
- Android获取所有安装的应用
- android 获取系统安装的所有的浏览器应用
- 获得所有安装的应用,复杂ListView的显示
- Android 获取所有已安装应用信息(图标,名称,版本号,包) ,并在自己程序打开某个应用
- unity3d 获取所有安装应用包名和应用名 android C#版
- android初学------获取所有安装的应用信息
- Android ListView从网络获取图片及文字显示
- 安卓开发获取本机所有已安装程序(形成ListView)
- 获得Android系统所有已安装的应用并联网自动检测升级更新
- Android获取系统已安装应用软件程序(apk)信息
- 【Android】获取手机中已安装apk文件信息(PackageInfo、ResolveInfo)(应用图片、应用名、包名等)
- Android获取已安装应用信息(图标,名称,版本号,包)
- Android 如何完整的获取到用户已安装应用列表