3

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

我有这个片段。`和路径| 是 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返回我null

有谁知道我在这里想念什么?

4

5 回答 5

6

这是您可以从not yet installedAPK 文件中获取应用程序标签的方法:

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

applicationInfoSDK 8 之前的版本无需打补丁。更多详情请查看此问题

于 2015-05-20T11:42:28.013 回答
4

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

它返回 ApplicationInfo

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

它返回应用程序名称

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

    }
于 2012-09-11T04:35:30.327 回答
0
public String applicationLabel (ApplicationInfo info){
     PackageManager p = con.getPackageManager();
     return p.getApplicationLabel(info).toString();
}

我希望你得到应用程序信息,将应用程序信息传递给这个函数,它将给出应用程序名称。

于 2012-09-11T04:09:46.400 回答
0
String title = getResources().getString(R.string.app_name);
于 2014-12-08T15:34:57.047 回答
0

只需使用正则表达式获取名称并与应用程序标签进行比较。

于 2020-02-11T06:59:40.373 回答