Android的:如何让apk文件编程的名字吗?名字、文件、Android、apk

2023-09-13 01:43:54 作者:吃完辣條好狂燥!

由于种种原因,我的应用程序,可以用不同的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" />