Tag Archives: android widgets

Get info about all installed applications in your Android Phone?/How to get the APK file of an installed application programatically?

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;
}
The android SDK provides the class PackageManager which retrieves various kinds of information related to the application packages that are currently installed on the device.

 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;
}
}
view raw Utils.java hosted with ❤ by GitHub
Application should required following uses permissions:

<!-- 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

Android: Some Cool and Useful Intents For Your App

Important intents and their usage..

mcochin

I was just playing around with some INTENTS and wanted to share some useful things they can do.

There are a lot more Intents to be discovered, but maybe this will give you insight on what you can do with your app.

View original post

Android ViewPager and Performance Improvements

Hello Everyone,
Hope you all are doing well. After seeing a threading concept in last post, Thread and Thread interruption in Java Today I would like to share some tricks while using the ViewPageIndicator in android application. So lets start it with following question,

My Activity has swipe-able tabs and each tab calls a different fragment. But when i swipe between tabs the transition seems a quite slow because of the destruction of the fragment view, that is rebuilded during the swipe operation.

The ViewPager doesn’t create all its pages at once. When using a lot of pages this would be horribly slow and even unnecessary if the user would never swipe through all these pages. By default the ViewPager only creates the current page as well as the off-screen pages to the left and right of the current page.

offscreen_pages

So what is solution of this problem, Is their any?

Hussshhh.. yeah there is,

 mViewPager.setOffscreenPageLimit(int limit);

If you only use a small amount of pages you may get a better performance by creating them all at once. You can use setOffscreenPageLimit(int limit) to set the number of pages that will be created and retained. With above setting to the pager loads a maximum of 3 pages (fragments) at the time: the one displaying, previous and next so if you have 5 fragments this will happen while you move from first to last: (where X is a loaded fragment)

XX000 -> XXX00 -> 0XXX0 -> 00XXX -> 000XX

If you have more than three tabs/fragments then try to using

 myPager.setOffscreenPageLimit(ITEMS_COUNT-1);

This will tell the pager to keep all of them in memory and not destroy/create with every swipe. But you need to KEEP A CLOSE LOOK ON THE MEMORY MANAGEMENT. For example, there are many many items with image and animations. If you cache all of them, this probably will result with an OutOfMemoryError. To prevent, you can define page limit to be cached.


MINIMUM THRESHOLD :
ViewPager require a minimum of 1 offscreen pages

If you set value to 0 then you should be getting a warning about this in LogCat, something like: Requested offscreen page limit0 too small; defaulting to1
Its because ViewPager is based on concept of keep ready next page to be loaded. ViewPager can not the page which not exist still.

For more close and precise look on this method,
public void setOffscreenPageLimit (int limit)

Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed.
This is offered as an optimization. If you know in advance the number of pages you will need to support or have lazy-loading mechanisms in place on your pages, tweaking this setting can have benefits in perceived smoothness of paging animations and interaction. If you have a small number of pages (3-4) that you can keep active all at once, less time will be spent in layout for newly created view subtrees as the user pages back and forth.
You should keep this limit low, especially if your pages have complex layouts. This setting defaults to
Parameters : limit – How many pages will be kept offscreen in an idle state.

Conclusion
So while using ViewPager in Android, if you have a small amount of pages you may get a better performance by creating them all at once and you can also use setOffscreenPageLimit(int limit) to set the number of pages that will be created and retained. But you also need to take care of Memory need if there are more graphics uses.

Hope this is helpful to you! Happy coding..
Stay tuned.

Swapnil Sonar