如何以编程方式获得APK的应用程序名称(未安装)。[英] How to get the application name of the APK programmatically (not installed)

本文是小编为大家收集整理的关于如何以编程方式获得APK的应用程序名称(未安装)。的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

基于如何获取应用程序的名称在Android?.

我有这个片段. `Andpath |是APK的位置.

PackageInfo info = getPackageManager().getPackageArchiveInfo(andPath, 0);
ApplicationInfo appinfo = info.applicationInfo;
String packageName = getPackageManager().getApplicationLabel(appinfo).toString();

PackageManager pm=getPackageManager();
ApplicationInfo ai;
try {
     ai = pm.getApplicationInfo(packageName, 0);
} catch (final NameNotFoundException e) {
     ai = null;
}

final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");

applicationName返回包名称.

CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName,PackageManager.GET_META_DATA));

c返回me null.

有谁知道我在这里遗漏了什么?

推荐答案

如果在这里有问题,我会发布完整代码以获取应用程序名称

它的返回applicationInfo

public List<ApplicationInfo> getApplicationList(Context con){
        PackageManager p = con.getPackageManager(); 
        List<ApplicationInfo> info = p.getInstalledApplications(0);
        return info;
    }

它返回applicationname

public String applicationLabel(Context con,ApplicationInfo info){
        PackageManager p = con.getPackageManager();
        String label = p.getApplicationLabel(info).toString();
        return label;
    }

在oncreate函数上粘贴以下代码

List<ApplicationInfo> info = new ArrayList<ApplicationInfo>();

        info = list.getApplicationList(this);
        LinearLayout layout = (LinearLayout)findViewById(R.id.content);
        layout.setOrientation(1);
        int size = info.size();
        Log.d("size",String.valueOf(size));
        for(int i=0;i<size;i++){
            Log.d("Val of I",String.valueOf(i));
            String appname = list.applicationLabel(this,info.get(i)).toString();
            Log.d("AppName",appname);
            TextView tv = new TextView(this);
            tv.setText(appname);
            layout.addView(tv);
        }

    }

其他推荐答案

这是如何从not yet installed apk文件获取应用程序标签的方式:

public static String getAppLabel(PackageManager pm, String pathToApk) {  
    PackageInfo packageInfo = pm.getPackageArchiveInfo(pathToApk, 0);

    if (Build.VERSION.SDK_INT >= 8) {
        // those two lines do the magic:
        packageInfo.applicationInfo.sourceDir = pathToApk;
        packageInfo.applicationInfo.publicSourceDir = pathToApk;
    }

    CharSequence label = pm.getApplicationLabel(packageInfo.applicationInfo);
    return label != null ? label.toString() : null;
}

不需要在SDK之前修补applicationInfo 8.请参阅这个问题更多详细信息.

其他推荐答案

public String applicationLabel (ApplicationInfo info){
     PackageManager p = con.getPackageManager();
     return p.getApplicationLabel(info).toString();
}

我希望您有应用程序信息通过该函数的应用程序信息它将提供应用程序名称.

本文地址:https://www.itbaoku.cn/post/102309.html

问题描述

Based How to get the name of the application in android?.

I have this snippet. `andPath| is the location of the APK.

PackageInfo info = getPackageManager().getPackageArchiveInfo(andPath, 0);
ApplicationInfo appinfo = info.applicationInfo;
String packageName = getPackageManager().getApplicationLabel(appinfo).toString();

PackageManager pm=getPackageManager();
ApplicationInfo ai;
try {
     ai = pm.getApplicationInfo(packageName, 0);
} catch (final NameNotFoundException e) {
     ai = null;
}

final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");

applicationName returns me the package name.

CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName,PackageManager.GET_META_DATA));

c returns me null.

Does anyone know what I'm missing here?

推荐答案

if you have a problem here i post the full code to get the application name

its returns ApplicationInfo

public List<ApplicationInfo> getApplicationList(Context con){
        PackageManager p = con.getPackageManager(); 
        List<ApplicationInfo> info = p.getInstalledApplications(0);
        return info;
    }

it returns ApplicationName

public String applicationLabel(Context con,ApplicationInfo info){
        PackageManager p = con.getPackageManager();
        String label = p.getApplicationLabel(info).toString();
        return label;
    }

on your onCreate function paste the below code

List<ApplicationInfo> info = new ArrayList<ApplicationInfo>();

        info = list.getApplicationList(this);
        LinearLayout layout = (LinearLayout)findViewById(R.id.content);
        layout.setOrientation(1);
        int size = info.size();
        Log.d("size",String.valueOf(size));
        for(int i=0;i<size;i++){
            Log.d("Val of I",String.valueOf(i));
            String appname = list.applicationLabel(this,info.get(i)).toString();
            Log.d("AppName",appname);
            TextView tv = new TextView(this);
            tv.setText(appname);
            layout.addView(tv);
        }

    }

其他推荐答案

This is how you can get the application label from not yet installed APK file:

public static String getAppLabel(PackageManager pm, String pathToApk) {  
    PackageInfo packageInfo = pm.getPackageArchiveInfo(pathToApk, 0);

    if (Build.VERSION.SDK_INT >= 8) {
        // those two lines do the magic:
        packageInfo.applicationInfo.sourceDir = pathToApk;
        packageInfo.applicationInfo.publicSourceDir = pathToApk;
    }

    CharSequence label = pm.getApplicationLabel(packageInfo.applicationInfo);
    return label != null ? label.toString() : null;
}

No need to patch the applicationInfo prior to SDK 8. See this issue for more details.

其他推荐答案

public String applicationLabel (ApplicationInfo info){
     PackageManager p = con.getPackageManager();
     return p.getApplicationLabel(info).toString();
}

I hope you got application info pass that application info to this function it will give application name.