Hello Guys,
I hope your day is going well. In today’s tutorial we will see how to retrieve the list of all installed applications and their info in android device. This tutorial also demonstrate, how access APK file from installed application. I have created PackageInfoBean to store detail information of the application.
package in.google.android.sample; | |
import android.content.pm.ApplicationInfo; | |
import android.graphics.drawable.Drawable; | |
/** | |
* @author Swapnil Sonar | |
* @Date 01/07/2015 | |
*/ | |
public class PackageInfoBean { | |
public CharSequence mAppName = ""; | |
public String mAppPackageName = ""; | |
public String mVersionName = ""; | |
public int mVersionCode = 0; | |
public Drawable icon; | |
public String mDataDir = ""; | |
public long cacheSize = 0; | |
public ApplicationInfo mApplicationInfo; | |
public String mApkFilePath = ""; | |
public long mApkFileSize = 0; | |
} |
List apps = getPackageManager().getInstalledPackages(0);
This method returns the list of PackageInfo which contains overall information about the contents of a package). With help of PackageInfo we have to easily access to ApplicationInfo, which corresponds to information collected from the AndroidManifest.xml’s tag. ApplicationInfo.sourceDir is the path to the .apk file. As per the developer’s documentation, ApplicationInfo.publicSourceDir is
public String publicSourceDir Full path to the publicly available parts of sourceDir, including resources and manifest. This may be different from sourceDir if an application is forward locked.
Now go on and try this.
package in.google.android.sample; | |
import java.io.File; | |
import java.util.ArrayList; | |
import java.util.List; | |
import android.content.Context; | |
import android.content.pm.ApplicationInfo; | |
import android.content.pm.PackageInfo; | |
import android.util.Log; | |
/** | |
* @author Swapnil Sonar | |
* @Date 01/07/2015 | |
*/ | |
public class Utils { | |
private static final String TAG = "Utils"; | |
/** | |
* It return all installed applications and along with its details. | |
* @param context | |
* @param getSysPackages | |
* @return | |
*/ | |
public static List<PackageInfoBean> getInstalledApps(Context context, boolean getSysPackages) { | |
List<PackageInfoBean> mPackageInfos = new ArrayList<PackageInfoBean>(); | |
long timeStamp = System.currentTimeMillis(); | |
List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(0); | |
for (int i = 0; i < packs.size(); i++) { | |
PackageInfo packageInfo = packs.get(i); | |
ApplicationInfo appInfo = packageInfo.applicationInfo; | |
if (((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1) && !getSysPackages) { | |
//NOP | |
}else{ | |
PackageInfoBean newInfo = new PackageInfoBean(); | |
newInfo.mAppName = appInfo.loadLabel(context.getPackageManager()); | |
newInfo.mAppPackageName = packageInfo.packageName; | |
newInfo.mDataDir = appInfo.dataDir; | |
// .apk file of application | |
newInfo.mApkFilePath = appInfo.publicSourceDir; | |
// apk file size | |
File file = new File(newInfo.mApkFilePath); | |
newInfo.mApkFileSize = file.length(); | |
newInfo.mVersionName = packageInfo.versionName; | |
newInfo.mVersionCode = packageInfo.versionCode; | |
newInfo.mApplicationInfo = packageInfo.applicationInfo; | |
newInfo.icon = appInfo.loadIcon(context.getPackageManager()); | |
mPackageInfos.add(newInfo); | |
} | |
} | |
long timeInterval = System.currentTimeMillis() - timeStamp; | |
Log.e(TAG, "getInstalledApps used: " + timeInterval); | |
return mPackageInfos; | |
} | |
} |
<!-- PERMISSIONS REQUIRED --> <uses-permission android:name="android.permission.GET_PACKAGE_SIZE" /> <uses-permission android:name="android.permission.INSTALL_PACKAGES" />
Now you call the following method to get the installed application information. The method requires two parameters, context – Application Context getSysPackages – true if you want to include system applications, false otherwise (other than system applications)
List<PackageInfoBean> list = Utils.getInstalledApps(MainActivity.this, false); for (PackageInfoBean bean : list) { System.out.println(bean.mAppName +" "+ bean.mAppPackageName); // do your stuff.. }
Hope this will helpful for you. Feel free to add your valuable comments.
Happy coding! Stay tuned..
Swapnil Sonar