Android的:如何让apk文件编程的名字吗? [英] Android: how to get the name of apk file programmatically?

查看:80
本文介绍了Android的:如何让apk文件编程的名字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于种种原因,我的应用程序,可以用不同的APK安装的名称。发射时,我想知道什么是apk文件的名称。你知道该怎么做?

For some reasons, my app can be installed with different apk names. At launch, I would like to know what is the name of the apk file. Do you know how to do that ?

谢谢!

推荐答案

试试这个:

ActivityManager actMngr = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
string runningPkg = actMngr.getRunningTasks(1).get(0).topActivity.getPackageName();

PackageManager pm = context.getPackageManager();
ApplicationInfo ai = pm.getApplicationInfo(runningPkg , 0);

补充:

如果你没有得到的apk的名字,那么你可以将其与龙头安装PKG进行比较,并得​​到相应的应用程​​序名称。您可以获取安装的应用程序的PKG名和应用程序名称是这样的:

If you are not getting the apk name then you can compare it with intalled pkg and get the corresponding app name. You can fetch the installed app's pkg name and app name like this:

ArrayList<PackageInfo> res = new ArrayList<PackageInfo>();
PackageManager pm = ctx.getPackageManager();
List<PackageInfo> packs = pm.getInstalledPackages(0);

for(int i=0;i<packs.size();i++) {
    PackageInfo p = packs.get(i);
    String description = (String) p.applicationInfo.loadDescription(pm);
    String  label= p.applicationInfo.loadLabel(pm).toString();
//Continue to extract other info about the app...
}

注:添加此权限的清单文件:

Note: Add this permission to the manifest file:

<uses-permission android:name="android.permission.GET_TASKS" />

这篇关于Android的:如何让apk文件编程的名字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆