用AsyncTask加载手机中已经安装的应用信息

最近在项目中一个小功能就是用ListView展示出手机中已经安装的应用信息,在网上找了一个Demo(感谢demo的分享者),然后根据自己的需要改动了一下,把改后的代码贴出来,留个笔记:

---------------先是应用程序的信息Bean------------------------

public class AppInfo implements Serializable {

private static final long serialVersionUID = 1L;

/**应用名称*/

public String apkName;

/**应用包名*/

public String packageName;

/**是用户还是系统应用*/

public boolean isSysApp;

/**应用大小*/

public long totalSize;

/**版本信息*/

public String versionInfo;

/**安装时间*/

public long installTime;

/**应用图标*/

public Drawable appIcon;

public AppInfo() {

}

public AppInfo(String appName, String packageName, String versionName, long length, long firstInstallTime, boolean isUserApp) {

super();

this.apkName = appName;

this.packageName = packageName;

this.versionInfo = versionName;

this.totalSize = length;

this.installTime = firstInstallTime;

this.isSysApp = isUserApp;

}

}

------------------列表Activity----------------

public class AppArrayActivity extends Activity {

private int systemCount;

private AppItemAdapter mAdapter;

private ProgressBar mProgressBar;

private ListView mListView;

private TextView mTitleTv;

private TextView mTotalTv;

private TextView mUserTv;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_applist);

findViewById();

getAppsData();

}

private void findViewById() {

mListView = (ListView) this.findViewById(R.id.app_lv);

mTitleTv = (TextView) this.findViewById(R.id.app_title_tv);

mTotalTv = (TextView) this.findViewById(R.id.app_total_tv);

mUserTv = (TextView) this.findViewById(R.id.app_user_tv);

mProgressBar = (ProgressBar) this.findViewById(R.id.app_pb);

}

private void getAppsData() {

new MainAsyncTask().execute();// 获取数据

}

/**异步加载获取手机中应用信息*/

private class MainAsyncTask extends AsyncTask<Void, Void, List<AppInfo>> {

protected void onPreExecute() {

mProgressBar.setVisibility(View.VISIBLE);

}

protected List<AppInfo> doInBackground(Void... params) {

return getAppInfo();

}

protected void onPostExecute(List<AppInfo> result) {

if (result != null) {

mAdapter = new AppItemAdapter(AppArrayActivity.this, result);

mListView.setAdapter(mAdapter);

mTitleTv.setText("所有应用:" + result.size());

mTotalTv.setText("系统应用:" + systemCount);

mUserTv.setText("用户应用:" + (result.size() - systemCount));

mProgressBar.setVisibility(View.GONE);

}

}

protected void onProgressUpdate(Void... values) {// 更新进度值

}

}

/**获取应用*/

@SuppressLint("NewApi")

private List<AppInfo> getAppInfo() {

List<AppInfo> list = new ArrayList<AppInfo>();

PackageManager packageManager = this.getPackageManager();

List<PackageInfo> packageInfo_list = null;

packageInfo_list = packageManager.getInstalledPackages(PackageManager.GET_CONFIGURATIONS);

int t = packageInfo_list.size();

systemCount = 0;

for (int i = 0; i < t; i++) {

PackageInfo packageInfo = packageInfo_list.get(i);

String packageName = packageInfo.packageName;

ApplicationInfo applicationInfo = packageInfo.applicationInfo;

String appName = applicationInfo.loadLabel(packageManager).toString();

/**是否是系统应用*/

boolean isUserApp = false;

if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) {

/**是系统应用,数量加*/

systemCount++;

}

else {

/**用户安装程序*/

isUserApp = true;

}

String versionName = packageInfo.versionName;

String sourceDir = applicationInfo.sourceDir;

long length = new File(sourceDir).length();

/**应用的安装时间*/

long firstInstallTime = packageInfo.firstInstallTime;

list.add(new AppInfo(appName, packageName, versionName, length, firstInstallTime, isUserApp));

}

return list;

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (requestCode == 0x01) {

getAppsData();

}

}

}

---------数据适配器---------------------

public class AppItemAdapter extends BaseAdapter {

private Context mContext;

private List<AppInfo> list;

private LayoutInflater layoutInflater;

public List<AppInfo> getList() {

return list;

}

public void setList(List<AppInfo> list) {

this.list = list;

}

public AppItemAdapter(Context context, List<AppInfo> list) {

this.mContext = context;

this.list = list;

layoutInflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override

public int getCount() {

return this.list.size();

}

@Override

public AppInfo getItem(int position) {

return this.list.get(position);

}

@Override

public long getItemId(int position) {

return position;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder viewHolder = null;

if (convertView == null) {

viewHolder = new ViewHolder();

convertView = this.layoutInflater.inflate(R.layout.activity_applist_listview_item, parent, false);

viewHolder.appIcon = (ImageView) convertView.findViewById(R.id.app_item_icon);

viewHolder.appName = (TextView) convertView.findViewById(R.id.app_item_name);

viewHolder.appSize = (TextView) convertView.findViewById(R.id.app_item_size);

viewHolder.appVersion = (TextView) convertView.findViewById(R.id.app_item_version);

viewHolder.appTime = (TextView) convertView.findViewById(R.id.app_item_time);

convertView.setTag(viewHolder);

}

else {

viewHolder = (ViewHolder) convertView.getTag();

}

AppInfo appInfo = this.list.get(position);

IconAsyncTask loadAppIconAsyncTask = new IconAsyncTask(mContext, viewHolder.appIcon, appInfo);

loadAppIconAsyncTask.execute();

viewHolder.appName.setText(appInfo.apkName);

viewHolder.appVersion.setText("版本:" + appInfo.versionInfo);

viewHolder.appSize.setText("大小:" + Formatter.formatFileSize(mContext, appInfo.totalSize));

viewHolder.appTime.setText("安装时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(appInfo.installTime));

return convertView;

}

private static class ViewHolder {

private ImageView appIcon;

private TextView appName;

private TextView appVersion;

private TextView appSize;

private TextView appTime;

}

private final class IconAsyncTask extends AsyncTask<Void, Void, Drawable> {

private Context context;

private ImageView mImageView;

private AppInfo appInfo;

public IconAsyncTask(Context context, ImageView imageView1, AppInfo appInfo) {

this.context = context;

this.mImageView = imageView1;

this.appInfo = appInfo;

}

protected void onPreExecute() {

this.mImageView.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_launcher));

}

protected Drawable doInBackground(Void... params) {

return getIcon();

}

protected void onPostExecute(Drawable drawable) {

if (drawable != null)

this.mImageView.setImageDrawable(drawable);

else

this.mImageView.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_launcher));

}

protected void onProgressUpdate(Void... values) {

}

private Drawable getIcon() {

Drawable drawable;

try {

PackageManager packageManager = context.getPackageManager();

PackageInfo packageInfo;

packageInfo = packageManager.getPackageInfo(appInfo.packageName, 0);

ApplicationInfo applicationInfo = packageInfo.applicationInfo;

drawable = applicationInfo.loadIcon(packageManager);

}

catch (NameNotFoundException e) {

e.printStackTrace();

return null;

}

return drawable;

}

}

}

时间: 2024-10-09 22:58:33

用AsyncTask加载手机中已经安装的应用信息的相关文章

【Android】获取手机中已安装apk文件信息(PackageInfo、ResolveInfo)(应用图片、应用名、包名等)

众所周知,通过PackageManager可以获取手机端已安装的apk文件的信息,具体代码如下 [java] view plaincopyprint? PackageManager packageManager = this.getPackageManager(); List<PackageInfo> packageInfoList = packageManager.getInstalledPackages(0); 通过以上方法,可以得到手机中安装的所有应用程序,既包括了手动安装的apk包的信

Android加载手机磁盘上的资源---decodeFile方法的使用

一般在写Android程序时,通常会将图片资源放在/res/drawable/文件夹下,读取时,通过R.drawable.imageId即可读取图片内容,但用户在使用时,一般会想要读取存放在存储卡上的资源,这时候上面的方法将不起作用,这时候,就需要使用Bitmap和BitmapFactory对象,来加载手机磁盘上的资源了. 首先在布局文件里放一个ImageView,用户放置图片,图片存放的路径为/data/data/demo.jpg,在程序中首先获取ImageView,代码如下: ImageVi

VMware 克隆虚拟机或加载新的已安装虚拟机时System eth0不能使用的解决方法

近年来的大数据应用特别热,特别是Hadoop和Spark.但大家使用这些分布式文件系统和计算框架都需要一个分布式的集群环境,而大家手头一般没有多余的机器部署master和多个slave节点,就只能在VMware上多安装几个虚拟机来模拟集群的搭建,但是安装好一台虚拟机后,我想大部分的人都不想再耗时再重复地去装一台虚拟机,所以有以下两种选择: (1)使用VMware的克隆功能: (2)复制一份已安装好的虚拟机数据,然后再将*.vmx文件加载到VMware Workstation中. 任选以上方法都可

weex 加载 .xcassets 中的图片资源

背景 因为 .xcassets 中的图片资源只能通过 imageNamed: 方法加载,所以需要做一些特殊处理,才能提供给 Weex 使用(PS:纯属娱乐,因为 Weex 跨平台的特性,这种针对某一端做实现的方案实用价值并不大). 方案 观察 WeexSDK 发现有 WXImgLoaderProtocol 这个协议,这个协议包含了下面的方法: - (id<WXImageOperationProtocol>)downloadImageWithURL:(NSString *)url imageFr

Android插件化(二):使用DexClassLoader动态加载assets中的apk

Android插件化(二):使用DexClassLoader动态加载assets中的apk 简介 上一篇博客讲到,我们可以使用MultiDex.java加载离线的apk文件.需要注意的是,apk中的类是加载到当前的PathClassLoader当中的,如果apk文件过多,可能会出现ANR的情况.那么,我们能不能使用DexClassLoader加载apk呢?当然是可以的!首先看一下Doc文档. A class loader that loads classes from .jar and .apk

百度地图 在加载过程中,根据回调函数结果中第一个点设置为中心点

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.4"></script&g

加载器中的预编

if (_type == "js") {//js预编译 var _script = "_define(function(exports,module){\n"; _script += "var $parent = \"" + _basePath + "\";\n"; _script += data.replace(/require\(/g, "_require($parent,"); _

ArcEngine加载SDE中的栅格数据的问题

在加载矢量数据时直接转到IFeatureWorkspace接口即可,但是在加载栅格数据时要转到IRasterWorkspaceEx接口.效果图如下,双击即可添加到Globe中. 代码如下: public partial class SDEDataViewer : DevComponents.DotNetBar.Office2007Form { #region 私有变量 private IWorkspace sdeWorkSpace; //sde工作控件 private IGlobeControl

spring加载过程中jar包加载不了,解决方法

当我们在开发spring项目时,一般会将jar包放到webInf/lib下,这样是myeclipse自动将jar包加载到tomcat中webapps下,但是当我们新建一个lib文件夹的情况下,我们add building Path时就会出错,这时候我们有个技巧供使用. 1.项目上点击右键搜索de,找到deployment assembly 目的就是将此处添加的jar包添加到系统webINF/lib路径下 来自为知笔记(Wiz) spring加载过程中jar包加载不了,解决方法