Java Question How to wrap a class that extends another class in java

Mousa Najafi

Member
Licensed User
Longtime User
I have the following code should be wrapped to be used in b4a library while there is another class in library that I wrapped a initialize method in that .
B4X:
public class MyPushListener extends PusheListenerService {
      @Override
     public void onMessageReceived(JSONObject message) {
          // Your code
     }
}
class the implemented initialize method in it is:
B4X:
@activityobject
public class pushwrapp{
    public void initialize(BA ba){
        //Push is a class name that registers app to receive a notification from special web server
        Push.initialize(ba.activity,true);
   }
this class and method worked successfully but I am not sure how to wrap the class that extends another class in java library also what should be written instead of JSONObject in wrapper
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Does MyPushListener do anything that you need? Why not just have PushWrapp extend PushListenerService? That will make it simple for you to create this library. You can do something like this:
B4X:
public class PushWrap extends PushListenerService
{
     private BA ba = null;
     private String eventName;     

     public void Initialize(BA ba, String eventName)
     {
          this.ba = ba;
          this.eventName = eventName;
          Push.initialize(ba.activity, true);
     }

     @Override
     @BA.Hide
     public void onMessageReceived(JSONObject message) 
     {
       // Your code
       String messageString;   //convert message to messageString, somehow
       ba.raiseEvent2(null, true, eventName.toLowerCase() + "_messagereceived", false, messageString);  //This raises an event
     }
}
 

Mousa Najafi

Member
Licensed User
Longtime User
Thank you Roycefer.I will try this and reply the results.
would you tell, how would be the format of messagereceived event in B4A?
By your event code, does JSONObject will be available directly in B4a for parsing? or should I do something more to convert message to messageString as you commented above?

the Push class initialize method should be called in main activity in eclipse by following code:
B4X:
Push.initialize(this,true)
I achieved the same thing by declaring class as activity object as you saw in the above code.
that prevented me to Dim class in process globals. if I will succeed by your code that will be solved also.
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Push.initialize() will be called when you Initialize your PushWrap Object. So long as you Initialize your PushWrap Object in the main Activity, it should work, I think.

As for what to do with the JSONObject, that depends. You could see what the JSONObject's toString() method gives you. If it just gives you a neat JSON String, then you can use B4A's JSONParser to parse it into a Map inside your B4A code. If that's what it does, then I would just do this:
B4X:
@Override
@BA.Hide
public void onMessageReceived(JSONObject message) 
{
     // Your code
     String messageString = message.toString();
     ba.raiseEvent2(null, true, eventName.toLowerCase() + "_messagereceived", false, messageString); //This raises an event
}
 
Top