How to get installed application name in android?

笑着哭i 提交于 2019-11-30 07:01:12

问题


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.


回答1:


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()



回答2:


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));
}


来源:https://stackoverflow.com/questions/6838223/how-to-get-installed-application-name-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!