基于How to get the name of the application in 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
返回null
。
有谁知道我在这里缺少什么?
答案 0 :(得分:5)
这是您从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 8之前修补applicationInfo
。有关详细信息,请参阅this issue。
答案 1 :(得分:4)
如果您在此处遇到问题,请发布完整代码以获取应用程序名称
返回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);
}
}
答案 2 :(得分:0)
public String applicationLabel (ApplicationInfo info){
PackageManager p = con.getPackageManager();
return p.getApplicationLabel(info).toString();
}
我希望你有应用程序信息将该应用程序信息传递给该函数,它将给出应用程序名称。
答案 3 :(得分:0)
String title = getResources().getString(R.string.app_name);
答案 4 :(得分:0)
只需使用正则表达式即可获取名称并与应用标签进行比较。