Java Question raise an activity sub from java class without having control over its "Initialize" (so BA object is null ?)

abilio486software

Active Member
Licensed User
Hi,

I have a library (Activity kind), that does not have received an .Initialize from my B4A source code.

Hoever I pretend to call a sub (like raising an event) to the activity that is on screen.

I'm getting an error when call
B4X:
if (BA.subExists( "pushy_msg")) {
   BA.raiseEventFromUI(this, "pushy_msg", notificationText);
}
telling that "ba" is null.

Error message:
B4X:
{message=Ola!!!!}
Invoking push receiver via reflection: com.teste.PushReceiver2
MSG:Ola!!!!
Invoking push receiver via reflection failed
java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at me.pushy.sdk.model.PushyBroadcastReceiver.execute(PushyBroadcastReceiver.java:22)
    at me.pushy.sdk.util.PushyBroadcastManager.sendBroadcast(PushyBroadcastManager.java:158)
    at me.pushy.sdk.util.PushyBroadcastManager.publishNotification(PushyBroadcastManager.java:99)
    at me.pushy.sdk.util.PushyMqttConnection.messageArrived(PushyMqttConnection.java:251)
    at me.pushy.sdk.lib.paho.internal.CommsCallback.deliverMessage(CommsCallback.java:475)
    at me.pushy.sdk.lib.paho.internal.CommsCallback.handleMessage(CommsCallback.java:379)
    at me.pushy.sdk.lib.paho.internal.CommsCallback.run(CommsCallback.java:183)
    at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean anywheresoftware.b4a.BA.subExists(java.lang.String)' on a null object reference
    at com.teste.PushReceiver2.onReceive(PushReceiver2.java:58)
    ... 9 more

How can I use ba from an external class that is automatically initialized not by me but from an external SDK?

How to force BA to evoke the current activity?
 

abilio486software

Active Member
Licensed User
I'm implementing Pushy.me and it's working but at this stage without triggering to the activity.
For this project I would opt to usr Pushy.me for several reasons.
It is possible to declare on a Java public var the actual BA context and use it from my library?
 

abilio486software

Active Member
Licensed User
Hi Erel, It's working without BA context or raise event.

I found with Reflection the way to call a B4A activity method from a pure java class:

B4X:
        // "notificationText" is the String with the notification text to send to main activity
        BA.Log("MSG:" + notificationText );
        // Locate the activity
        ActivityManager activityManager = (ActivityManager)    context.getSystemService("activity");
        List<ActivityManager.RunningTaskInfo> taskInfo = activityManager.getRunningTasks(1);
        String currentTopActivity = taskInfo.get(0).topActivity.getClassName();
        // This is the top activity
        BA.Log("MSG ACTIVITY:" + currentTopActivity );
        // Try to instance the object to call the remote method
        try {
            Class myClass = Class.forName(currentTopActivity); 
            Activity act;
            act = (Activity) myClass.newInstance();
            //Get the list of possible methods held in this object.
            Method[] methods = myClass.getMethods();

            // iterate through them
            //for (Method method : methods) {
            //  BA.Log("" + this.getClass().getName() + "--------------------------");
            // BA.Log("" + this.getClass().getName()+ "Method: " + method.getName());
            // BA.Log("" + this.getClass().getName()+ "Return Type: " + method.getReturnType());
            // BA.Log("" + this.getClass().getName()+ "Class: " + method.getClass());
            // BA.Log("" + this.getClass().getName()+ "Declaring Class: " + method.getDeclaringClass());
            //}
            // BA.Log("" + this.getClass().getName()+"--------------------------");
       
                // But I now exactly the method I want
            Method setNameMethod = myClass.getMethod("_recebeu_mensagem", String.class);
            // Now call the method of main activity 
            setNameMethod.invoke(act, notificationText); // pass arg
 
      }
      catch( Exception e){
        
          BA.Log("ERRO!!! = " + e);
      }

I hope it can be usefull for someone else :)

Thanks,

Abilio
 
Top