用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;
}

}

}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。