Thursday 12 May 2016

All installed apps information

We can get all the information about all the application installed on your android device.

Following  is the code using which we can get information of app like application name, size, app icon etc


1.Create a model class to store application information like app name, size icon etc
Following is the sample model class

DataDto.java

import java.io.File;

import android.graphics.drawable.Drawable;


public class DataDTO {
   public Drawable imgAppIcon;
   public String txtAppName;
   public String txtAppSize;
   public boolean txtAppStatus;
   public File file;
   public File getFile() {
      return file;
   }
   public void setFile(File file) {
      this.file = file;
   }
   public Drawable getImgAppIcon() {
      return imgAppIcon;
   }
   public void setImgAppIcon(Drawable drawable) {
      this.imgAppIcon = drawable;
   }
   public String getTxtAppName() {
      return txtAppName;
   }
   public void setTxtAppName(String txtAppName) {
      this.txtAppName = txtAppName;
   }
   public String getTxtAppSize() {
      return txtAppSize;
   }
   public void setTxtAppSize(String txtAppSize) {
      this.txtAppSize = txtAppSize;
   }
   public boolean getTxtAppStatus() {
      return txtAppStatus;
   }
   public void setTxtAppStatus(boolean txtAppStatus) {
      this.txtAppStatus = txtAppStatus;
   }

}

2.Using package manager we can extract all the application information installed on your android device.

ArrayList<DataDTO> list = new ArrayList<DataDTO>();

final PackageManager packageManager = getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final List pkgAppsList = packageManager.queryIntentActivities( intent, 0);
for (Object object : pkgAppsList) {

   ResolveInfo info = (ResolveInfo) object;

   //Get application apk   File f1 =new File( info.activityInfo.applicationInfo.publicSourceDir);

   Log.v("file--", " "+f1.getName().toString()+"----"+info.loadLabel(packageManager));

   String file_name = info.loadLabel(packageManager).toString();
   DataDTO dataDTO = new DataDTO();

   dataDTO.setTxtAppStatus(isPresent(file_name));

   //Get application name   dataDTO.setTxtAppName(file_name);

   //Get application icon   dataDTO.setImgAppIcon(info.loadIcon(packageManager));

   int size= (int) (f1.length()/1024);

   //Get application size   if( size <=1024 ) {
      dataDTO.setTxtAppSize(size+" KB");
   } else {
      float a = (float) (size/1024.00);
      double newKB = Math.round(a*100.0)/100.0;
      dataDTO.setTxtAppSize(newKB+" MB");
   }
   dataDTO.setFile(f1);

   list.add(dataDTO);
}



note : For better performance you can execute this code in an asynctask



3.You can sort the list of application information as per your requirement
following is a example of sorting by app name

//SortingCollections.sort(list, new Comparator<DataDTO>() {
   @Override   public int compare(DataDTO  obj1, DataDTO  obj2)
   {
      return  obj1.txtAppName.compareTo(obj2.getTxtAppName());
   }
});


Thanks :)

My Apps MobileUtility

No comments:

Post a Comment