11

I have made a small method to display the installed application name in android. But when i give "name" attribute its showing exception error. And when i give "packageName" the method executes perfectly and displays the package name in a list

private void getInstalledApps() {
    // TODO Auto-generated method stub
     PackageManager packageManager=this.getPackageManager();
        List<ApplicationInfo applist=packageManager.getInstalledApplications(0);


        Iterator<ApplicationInfo> it=applist.iterator();
        while(it.hasNext()){
            ApplicationInfo pk=(ApplicationInfo)it.next();

            String appname=pk.name.toString();

            installedapplist.add(appname);
        }

}

In the above code when i give String appname=pk.packageName.toString() it works fine but when I give String appname=pk.name.toString() the program is throwing an exception error. Please help me to sort out the problem.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Unnikrishnan
  • 501
  • 2
  • 9
  • 18

2 Answers2

27

My guess is your code is throwing a NullPointerException because the name field is null. In any event, what you probably want is:

String appname = packageManager.getApplicationLabel(pk).toString()
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Thanks. Exactly what I was looking for. – user3734429 Oct 01 '15 at 10:04
  • @charlag_khan - You mean `pk.name`? Probably because the manifest doesn't declare an `android:name` attribute. That name is only used when the app needs to define it's own `Application` subclass. – Ted Hopp Apr 09 '17 at 14:45
0

by using this you can get installed app package names and app names

List<PackageInfo> packageInfos=getPackageManager().getInstalledPackages(0);
for (PackageInfo packageInfo:packageInfos)
{
        Log.d(TAG,"packageName "+packageInfo.packageName);
        Log.d(TAG,"appname "+getPackageManager().getApplicationLabel(packageInfo.applicationInfo));
}
saigopi.me
  • 14,011
  • 2
  • 83
  • 54