English 中文(简体)
Android:如何从方案角度获得纸浆档案的名称?
原标题:Android: how to get the name of apk file programmatically?

出于某种原因,我的手表可以安装不同的纸面名称。 在发射时,我想知道猪肉档案的名称是什么。 你们是否知道如何这样做?

Thanks !

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

您可以:

info.versionCod

以及

info.versionName

希望你们能帮助你们

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

假设你可以提出猪肉。

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

尝试:

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

添加:

如果你没有拿到纸面名称,那么你可以将其与全数公斤进行比较,并获得相应的用名称。 你们可以打上安装的 app子,并用同样的名称:

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" />

通过以下网址实现:getPackageArchiveInfo,从一个包裹式的纸面文件:

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

令我感到担忧的是:

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
                
            




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签