Use AsyncTask to load the app information that has been installed on the mobile phone, and asynctask to load

Source: Internet
Author: User

Use AsyncTask to load the app information that has been installed on the mobile phone, and asynctask to load

Recently, a small feature in the project is to use ListView to display the installed app information on the mobile phone. I found a Demo on the Internet (thanks to the sharer of the demo ), then, according to your own needs, I changed the code and posted the code. I will take a note:

--------------- The application information Bean ------------------------

Public class AppInfo implements Serializable {
Private static final long serialVersionUID = 1L;
/** Application name */
Public String apkName;
/** Application package name */
Public String packageName;
/** User or system application */
Public boolean isSysApp;
/** Application size */
Public long totalSize;
/** Version information */
Public String versionInfo;
/** Installation time */
Public long installTime;
/** Application icon */
Public Drawable appIcon;

Public AppInfo (){
}

Public AppInfo (String appName, String packageName, String versionName, long length, long firstInstallTime, boolean isUserApp ){
Super ();
This.apk Name = appName;
This. packageName = packageName;
This. versionInfo = versionName;
This. totalSize = length;
This. installTime = firstInstallTime;
This. isSysApp = isUserApp;
}

}

------------------ List 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 ();
Getaskdata ();

}

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 getaskdata (){
New mainasynctask(cmd.exe cute (); // get data
}

/** Asynchronously load and obtain app information on the mobile phone */
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 ("all applications:" + result. size ());
MTotalTv. setText ("System Application:" + systemCount );
MUserTv. setText ("user application:" + (result. size ()-systemCount ));
MProgressBar. setVisibility (View. GONE );
}
}

Protected void onProgressUpdate (Void... values) {// Update Progress Value
}
}

/** Get application */
@ 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 ();
/** Whether it is a system application */
Boolean isUserApp = false;
If (applicationInfo. flags & ApplicationInfo. FLAG_SYSTEM)> 0 ){
/** Is a system application, and the number is increased */
SystemCount ++;
}
Else {
/** User installer */
IsUserApp = true;
}
String versionName = packageInfo. versionName;
String sourceDir = applicationInfo. sourceDir;
Long length = new File (sourceDir). length ();
/** Application installation time */
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 ){
Getaskdata ();
}
}

}

--------- Data adapter ---------------------

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.exe cute ();
ViewHolder.appName.setText(appInfo.apk Name );
ViewHolder. appVersion. setText ("version:" + appInfo. versionInfo );
ViewHolder. appSize. setText ("Size:" + Formatter. formatFileSize (mContext, appInfo. totalSize ));
ViewHolder. appTime. setText ("installation time:" + 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;
}

}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.