如何在android中获取已安装的应用程序名称?

发布于 2024-11-26 21:09:09 字数 755 浏览 2 评论 0原文

我做了一个小方法来显示android中已安装的应用程序名称。但是当我给出“name”属性时,它显示异常错误。当我给出“packageName”时,该方法完美执行并在列表中显示包名称在

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

}

上面的代码中,当我给出 String appname=pk.packageName.toString() 时,它工作正常,但是当我给出String appname=pk.name.toString() 程序抛出异常错误。请帮我解决这个问题。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

婴鹅 2024-12-03 21:09:09

我的猜测是您的代码抛出 NullPointerException 因为 name 字段为空。无论如何,您可能想要的是:

String appname = packageManager.getApplicationLabel(pk).toString()

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()
摘星┃星的人 2024-12-03 21:09:09

通过使用它,您可以获得已安装的应用程序包名称和应用程序名称

List<PackageInfo> packageInfos=getPackageManager().getInstalledPackages(0);
for (PackageInfo packageInfo:packageInfos)
{
        Log.d(TAG,"packageName "+packageInfo.packageName);
        Log.d(TAG,"appname "+getPackageManager().getApplicationLabel(packageInfo.applicationInfo));
}

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));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文