RSS

Android: How to get Installed Applications List from your Application

26 Jan

Class: InstalledAppData

public class InstalledAppData{
	private String deviceId;
	private String packageName;
	private String appName;
	private String installTime;
	private String status;

	public InstalledAppData(String deviceId, String packageName,
			String appName, long installTime, String status) {
		super();
		this.deviceId = deviceId;
		this.packageName = packageName;
		this.appName = appName;
		long time = installTime;
		Date date = new Date(time);
		SimpleDateFormat sdf = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String datetime = sdf.format(date);
		this.installTime = datetime;
		this.status = status;
	}
	public String getDeviceId() {
		return deviceId;
	}
	public void setDeviceId(String deviceId) {
		this.deviceId = deviceId;
	}
	public String getPackageName() {
		return packageName;
	}
	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}
	public String getAppName() {
		return appName;
	}
	public void setAppName(String appName) {
		this.appName = appName;
	}
	public long getInstallTime() {
		try {
			DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = (Date) formatter.parse(this.installTime);
			return date.getTime();

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	public void setInstallTime(long installTime) {
		long time = installTime;
		Date date = new Date(time);
		SimpleDateFormat sdf = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String datetime = sdf.format(date);
		this.installTime = datetime;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
}

Add the following methods in your Activity and show them in ListView using your custom ArrayAdapter
Method: importInstalledAppsData() returns an ArrayList of IntsalledAppData

public ArrayList<InstalledAppData> importInstalledAppsData(){
    ArrayList<InstalledAppData> appList=new ArrayList<InstalledAppData>();
    PackageManager pkgManager=getApplicationContext().getPackageManager();
    List<ApplicationInfo> packages=getInstalledApplicationsList(pkgManager);
    String deviceId=Util.getDeviceId(getApplicationContext());
    
    for (ApplicationInfo packageInfo : packages) {
	String packageName=packageInfo.packageName;
	String appName="";
	String appFile = packageInfo.sourceDir;
	long installTime = new File(appFile).lastModified();
	String status="s";
	if ((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
            appName = (String) pkgManager.getApplicationLabel(packageInfo);
        } 
        else {
	    appName = (String) pkgManager.getApplicationLabel(packageInfo);
	}
			
	InstalledAppData data=new InstalledAppData(deviceId, packageName, appName, installTime, status);
        appList.add(data);
	
		
    }
    return appList;
}

Method: getInstalledApplicationsList returns List of ApplicationInfo

public List<ApplicationInfo> getInstalledApplicationsList(PackageManager pm) {
    List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
    return packages;
}
 
2 Comments

Posted by on January 26, 2012 in Uncategorized

 

2 responses to “Android: How to get Installed Applications List from your Application

  1. Ashish

    January 23, 2013 at 11:33 am

    hey thnx 4 this
    it is really hlpfl 4 me

     
  2. tom

    September 6, 2013 at 7:16 am

    Hi, is it possible for you to upload the project file? I can’t get those codes to work, thx

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: