Android:如何以编程方式获取apk文件的名称?

发布于 2024-12-07 12:10:55 字数 84 浏览 0 评论 0原文

由于某些原因,我的应用程序可以使用不同的 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 ?

Thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

淡笑忘祈一世凡恋 2024-12-14 12:10:56

试试这个它对我有用:

String versionName = "null"; //String apk versionName
                int versionCode = -1; //int versionCode
                String APKFilePath = "/storage/emulated/0/apkname.apk"; //For example
                PackageManager pm = getPackageManager();
                PackageInfo    packageInfo = pm.getPackageArchiveInfo(APKFilePath, 0);
                // the secret are these two lines
                packageInfo.applicationInfo.sourceDir       = APKFilePath;
                packageInfo.applicationInfo.publicSourceDir = APKFilePath;
                Drawable APKicon = packageInfo.applicationInfo.loadIcon(pm); //drawable apk icon
                String   AppName = (String)packageInfo.applicationInfo.loadLabel(pm); // String apk name
                    versionName = packageInfo.versionName; //String version name
                    versionCode = packageInfo.versionCode; //int version code
                    apk_icon.setImageDrawable(APKicon);    //ImageView apk icon
                    Apkpackage.setText(packageInfo.packageName); //TextView apk package name
                    Apkname.setText(AppName); //TextView apk name
                    Apkversion.setText(String.format("Version Name: %s Version Code:%d", versionName, versionCode)); //TextView versionName and versionCode
                
            

Try this it work for me:

String versionName = "null"; //String apk versionName
                int versionCode = -1; //int versionCode
                String APKFilePath = "/storage/emulated/0/apkname.apk"; //For example
                PackageManager pm = getPackageManager();
                PackageInfo    packageInfo = pm.getPackageArchiveInfo(APKFilePath, 0);
                // the secret are these two lines
                packageInfo.applicationInfo.sourceDir       = APKFilePath;
                packageInfo.applicationInfo.publicSourceDir = APKFilePath;
                Drawable APKicon = packageInfo.applicationInfo.loadIcon(pm); //drawable apk icon
                String   AppName = (String)packageInfo.applicationInfo.loadLabel(pm); // String apk name
                    versionName = packageInfo.versionName; //String version name
                    versionCode = packageInfo.versionCode; //int version code
                    apk_icon.setImageDrawable(APKicon);    //ImageView apk icon
                    Apkpackage.setText(packageInfo.packageName); //TextView apk package name
                    Apkname.setText(AppName); //TextView apk name
                    Apkversion.setText(String.format("Version Name: %s Version Code:%d", versionName, versionCode)); //TextView versionName and versionCode
                
            
暮光沉寂 2024-12-14 12:10:55
final PackageManager pm = getPackageManager();
String apkName = "example.apk";
String fullPath = Environment.getExternalStorageDirectory() + "/" + apkName;        
PackageInfo info = pm.getPackageArchiveInfo(fullPath, 0);

你可以通过以下方式得到它:

info.versionCod

希望

info.versionName

这对你有帮助

final PackageManager pm = getPackageManager();
String apkName = "example.apk";
String fullPath = Environment.getExternalStorageDirectory() + "/" + apkName;        
PackageInfo info = pm.getPackageArchiveInfo(fullPath, 0);

you can got it with:

info.versionCod

and

info.versionName

hope this help you

梦初启 2024-12-14 12:10:55

可以使用包管理器检索 apk 或应用程序名称。
根据亚历克斯在这里的回答 - https://stackoverflow.com/a/30348666/2026280

假设您有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;
}

The apk or app name can be retrieved using package manager.
According to alex's answer here - https://stackoverflow.com/a/30348666/2026280

Assuming you have the path of the 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;
}
凉月流沐 2024-12-14 12:10:55

试试这个:

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 名称和应用程序名称:

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

注意:将此权限添加到清单文件中:

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

try this:

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

Added:

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" />
花心好男孩 2024-12-14 12:10:55

这是通过 从已打包的 apk 文件中获取PackageArchiveInfo

  PackageInfo package_info = pm.getPackageArchiveInfo(file_path_to_apk, 0);

This is achieved with getPackageArchiveInfo from an already packaged apk file:

  PackageInfo package_info = pm.getPackageArchiveInfo(file_path_to_apk, 0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文