Android Question [Solved] #VersionName w/o PackageManager

udg

Expert
Licensed User
Longtime User
Hi all,

I'm trying to get running app's VersionNumber not using the PackageManager from Phone lib.
Tried with JavaObject then Reflection but failed.

Relevant references are:
- android.content.pm.PackageManager
- getPackageInfo
- versionName

Can you help, please? TIA.

udg
 

udg

Expert
Licensed User
Longtime User
Ok, found a way, but it looks it can be simplified and made more elegant:

B4X:
Dim r, r2 , r3 As Reflector
r.Target = r.GetActivity
r.Target = r.RunMethod("getApplicationContext")
r2.Target = r.RunMethod("getPackageManager")
r3.Target = r2.RunMethod3("getPackageInfo", "b4a.example", "java.lang.String", 0, "java.lang.int")
Dim s As String = r3.GetField("versionName")
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Probably this one is better:
B4X:
Dim jo As JavaObject
jo.InitializeContext
Dim s1 As String = jo.RunMethodjo("getPackageManager",Null).RunMethodJo("getPackageInfo", _
   Array As Object ("b4a.example", 0)).GetField("versionName")
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi Filippo.
yes it is absolutely the same and probabily safer, since I guess Erel will update its code as soon as Google will change its own.

I simply forgot about the existence of Application... :oops:
 
Upvote 0
Top