Android Question Check if apk is installed

imgsimonebiliato

Well-Known Member
Licensed User
Longtime 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?
 

DonManfred

Expert
Licensed User
Longtime User
Use Packagemanager to find out
 
Upvote 0

imgsimonebiliato

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

imgsimonebiliato

Well-Known Member
Licensed User
Longtime 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
 
Upvote 0

imgsimonebiliato

Well-Known Member
Licensed User
Longtime 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
 
Upvote 0

imgsimonebiliato

Well-Known Member
Licensed User
Longtime 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:
Upvote 0

imgsimonebiliato

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

DonManfred

Expert
Licensed User
Longtime 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
 
Upvote 0
Top