Android Question Check if apk is installed

Discussion in 'Android Questions' started by imgsimonebiliato, Feb 11, 2015.

  1. imgsimonebiliato

    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?
     
  2. DonManfred

    DonManfred Expert Licensed User

    Use Packagemanager to find out
     
    Erel likes this.
  3. Erel

    Erel Administrator Staff Member Licensed User

    Use PackageManager from the phone library.
     
  4. imgsimonebiliato

    imgsimonebiliato Well-Known Member Licensed User

    Thanks but if the apk doesn't exist, the application stop
     
  5. DonManfred

    DonManfred Expert Licensed User

    The look out for the error in the logs....

    The packagemanager works! The problem is in your code
     
  6. imgsimonebiliato

    imgsimonebiliato Well-Known Member Licensed User

    If I use
    Code:
    p.GetVersionName("it.android.test")
    and the apk with this package name, doesn't exist, I receive an error message
     
  7. DonManfred

    DonManfred Expert Licensed User

    And we should now guess WHAT ERROR you got?
     
  8. imgsimonebiliato

    imgsimonebiliato Well-Known Member Licensed User

    The error is that the device, can't found the package.
    Code:
    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
     
  9. imgsimonebiliato

    imgsimonebiliato Well-Known Member Licensed User

    I solved!
    Put it into a try catch like:

    Code:
    Try
      av = p.GetVersionName(
    "it.android.test)
    Catch
    Log("Apk not found")
    End Try
     
    Last edited: Feb 11, 2015
  10. imgsimonebiliato

    imgsimonebiliato Well-Known Member Licensed User

    Please, is it possible to retrieve the VersionName, of an apk that it isn't installed?
     
  11. Erel

    Erel Administrator Staff Member Licensed User

  12. imgsimonebiliato

    imgsimonebiliato Well-Known Member Licensed User

    damn!
    Thanks
     
  13. DonManfred

    DonManfred Expert Licensed User

    you dont need try block
    Code:
    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
     
  14. imgsimonebiliato

    imgsimonebiliato Well-Known Member Licensed User

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice