Android Question Check if apk is installed

imgsimonebiliato

Well-Known Member
Licensed User
Hello,
passing the package name of an apk (or other solutions), how can I check if the apk is already installed on the device or not?
 

imgsimonebiliato

Well-Known Member
Licensed User
Thanks but if the apk doesn't exist, the application stop
 

imgsimonebiliato

Well-Known Member
Licensed User
If I use
B4X:
p.GetVersionName("it.android.test")
and the apk with this package name, doesn't exist, I receive an error message
 

imgsimonebiliato

Well-Known Member
Licensed User
The error is that the device, can't found the package.
B4X:
android.content.pm.PackageManager$NameNotFoundException: it.android.test

    at android.app.ApplicationPackageManager.getPackageInfo(ApplicationPackageManager.java:80)
    at anywheresoftware.b4a.phone.PackageManagerWrapper.GetVersionName(PackageManagerWrapper.java:53)
    at it.android.imgspa.and_menu.main._ftp_listcompleted(main.java:486)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
    at anywheresoftware.b4a.BA$3.run(BA.java:320)
    at android.os.Handler.handleCallback(Handler.java:725)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:153)
    at android.app.ActivityThread.main(ActivityThread.java:5297)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    at dalvik.system.NativeStart.main(Native Method)
android.content.pm.PackageManager$NameNotFoundException: it.android.test
 

imgsimonebiliato

Well-Known Member
Licensed User
I solved!
Put it into a try catch like:

B4X:
Try
  av = p.GetVersionName("it.android.test)
Catch
Log("Apk not found")
End Try
 
Last edited:

imgsimonebiliato

Well-Known Member
Licensed User
Please, is it possible to retrieve the VersionName, of an apk that it isn't installed?
 

DonManfred

Expert
Licensed User
you dont need try block
B4X:
Sub ispackageinstalled(pkg As String)
    Dim pm As PackageManager
    Dim packages As List
    packages = pm.GetInstalledPackages
    Dim found As Boolean = False
    For i = 0 To packages.Size - 1
        Dim t As String = packages.Get(i)
        If t.ToUpperCase = pkg.ToUpperCase Then
            found = True
        End If
    Next
    Return found
End Sub
 
Top