12

I want to get Minecraft Pocket Edition's versionName (Example 0.13.0.b5).

This is package of MCPE (Minecraft Pocket Edition)

com.mojang.minecraftpe

I want to get version to String. How can I do this?

My code to get package name:

List<PackageInfo> packages = getPackageManager().getInstalledPackages(0);

PackageInfo mypackage = <com.mojang.minecraftpe>;

String versionName = mypackage.versionName;

Solution:

try
    {
        PackageInfo eInfo = getPackageManager().getPackageInfo("com.mojang.minecraftpe", 0);
        changelog=eInfo.versionName;
        lol.setText(changelog);
        }
    catch (PackageManager.NameNotFoundException e)
    {}

2 Answers2

23

It gets the package info from the package manager (by package) and returns the version name.:

PackageInfo packageInfo = getPackageManager().getPackageInfo("com.mojang.minecraftpe", 0);
return packageInfo.versionName;
Luca Ziegler
  • 3,236
  • 1
  • 22
  • 39
  • 3
    Welcome to StackOverflow! Please add some explanation to your code: why does it works? What should it do? – Aurasphere Nov 16 '15 at 10:04
  • 4
    it returns the version name of a specific installed application – Luca Ziegler Nov 16 '15 at 10:52
  • 1
    @Aurasphere it gets the package info from the package manager (by package) and returns the version name. I think it's somewhat self-explanatory. Any `Context` has a package manager. – EpicPandaForce Nov 16 '15 at 11:37
  • @EpicPandaForce "Short answers are ok, but full explanation is better", quoted from here: http://stackoverflow.com/help/how-to-answer – Aurasphere Nov 16 '15 at 11:41
2
 List<PackageInfo> apps = getPackageManager().getInstalledPackages(0);
 ArrayList<App> result = new ArrayList<>();

 for(int i=0;i<apps.size();i++) {
      PackageInfo p = apps.get(i);
      App newApp = new App();
      newApp.pname = p.packageName;
      newApp.versionName = p.versionName;
      newApp.versionCode = p.versionCode;
 }

 String minecraft = "com.mojang.minecraftpe";

 for (App name : result) {
        if (Objects.equals(minecraft, name.pname))
        String versionName = name.versionName;

 }

//----- class description ------

class App {
    String pname = "";
    String versionName = "";
    int versionCode = 0;
 }

If you want the version code, modify the 'if' condition in for loop with name.VersionCode.

Reference: PackageManager(Class for retrieving various kinds of information related to the application packages that are currently installed on the device.)

vishnu narayanan
  • 3,813
  • 2
  • 24
  • 28