Android Question InLine JavaCode

Discussion in 'Android Questions' started by MarcoRome, Feb 17, 2015.

  1. MarcoRome

    MarcoRome Expert Licensed User

    Hi all i have this code:
    Code:
    ....
    If FirstTime Then
    NativeMe.InitializeContext
    EndIf
    Dim w AsBoolean
    w = NativeMe.RunMethod(
    "isInstalled"Null' <----- I think that i have here ERROR
    Log("Whatsapp " & w)
    ......
    #If JAVA
    import java.io.File;
    import android.content.Context;
    import java.util.ArrayList;
    import android.content.ContentProviderOperation;
    import android.content.ContentResolver;
    import android.content.Intent;
    import android.content.pm.PackageInfo;
    import android.content.pm.PackageManager;
    import android.database.Cursor;
    import android.net.Uri;
    import android.provider.ContactsContract;
    import android.util.Log;
    private final static String WHATSSAP_APP_ID = "com.whatsapp";
    private final static boolean DEBUG = false;
        
    public final static int IMAGE_TYPE = 0;
    public final static int AUDIO_TYPE = 1;
    public final static int VIDEO_TYPE = 2;
        
        public final static boolean isInstalled(Context context){
             Boolean installed = false;
             try{
                 PackageManager mPm = context.getPackageManager();
                 PackageInfo info = mPm.getPackageInfo(WHATSSAP_APP_ID, 0);
                 installed = info != null;
             }catch(Exception e){
                 installed = false;
                 if(DEBUG) Log.e("Exception", e.toString());
             }
             return installed;
         }
    #End If
    compile and when start app i have this error:
    Code:
    ** Activity (main) Create, isFirst = true **
    java.lang.RuntimeException: Method: isInstalled 
    not matched.
        at anywheresoftware.b4j.object.JavaObject.RunMethod(
    JavaObject.java:128)
        at b4a.example.main._activity_create(main.java:
    351)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    515)
        at anywheresoftware.b4a.BA.raiseEvent2(BA.java:
    187)
        at b4a.example.main.afterFirstLayout(main.java:
    112)
        at b4a.example.main.access$
    100(main.java:29)
        at b4a.example.main$WaitForLayout.run(main.java:
    90)
        at android.os.Handler.handleCallback(Handler.java:
    733)
        at android.os.Handler.dispatchMessage(Handler.java:
    95)
        at android.os.Looper.loop(Looper.java:
    136)
        at android.app.ActivityThread.main(ActivityThread.java:
    5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:
    779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
    595)
        at dalvik.system.NativeStart.main(Native Method)
    java.lang.RuntimeException: Method: isInstalled 
    not matched.
    Any idea ?
    Thanks
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    The method shouldn't be static (in Activities).
     
  3. MarcoRome

    MarcoRome Expert Licensed User

    Ok, also if i write:

    Code:
    ...
    public boolean isInstalled(Context context){
    ....
    i have anyway this error:

    Code:
    java.lang.RuntimeException: Method: isInstalled not matched.
    maybe is wrong this code:

    Code:
    If FirstTime Then
    NativeMe.InitializeContext
    EndIf
    Dim w AsBoolean
    w = NativeMe.RunMethod(
    "isInstalled"Null' <----- I think that i have here ERROR
    Log("Whatsapp " & w)
    in this case --> isInstalled(Context context) is correct call this method with parameter NULL ?
     
  4. DonManfred

    DonManfred Expert Licensed User

    There are easier ways to find out if whatsapp is installed. Use Packagemanager
     
    thedesolatesoul, Erel and MarcoRome like this.
  5. MarcoRome

    MarcoRome Expert Licensed User

    Yes i know this Manfred. I'm conducting trials of this wonderful new opportunity to insert code jave directly.
    In library we substitute Context context --> BA ba.
    I would understand when i have "Context context" in code B4A is "Null" or what ??
    Anyway thanks for your continuous support to the community.
    Marco
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Remove the context parameter.

    You can use 'this' to access the activity (or context).
     
    thedesolatesoul and MarcoRome like this.
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