Beta Can we use ba.raiseEvent from inline java code?

stevel05

Expert
Licensed User
Longtime User
Is it possible to use ba.raiseEvent to call a callback sub in the enclosing Activity,class,service or code module? It doesn't give an error, just no result either.

In a class, I've tried:

B4X:
Sub DoMultiply
    nativeMe.RunMethod("Multiply", Array ("logresult",10))
End Sub

Sub LogResult(Result As Int)
    Log(Result)
End Sub

#If JAVA
private int mm = 100;
public void Multiply(String EventName,int i) {
    int result = i * 2 + mm;
    if(ba.subExists(EventName)){
        BA.Log("Found");
        ba.raiseEvent(this,EventName.toLowerCase(),result);
    } else {
        BA.Log("Not Found");
    }
}
#End If

The sub is found, but not called. result is an int and the target sub has one parameter which is an Int.

I can get the result I want by using conditional compilation and two subs that call the target sub directly with different signatures for Debug and Release modes. But I am hoping for a simple alternative.

Thanks
 

stevel05

Expert
Licensed User
Longtime User
I can't see a ba.callsub available, just checked the B4aShared from the Beta version with eclipse. Have I missed it?

The ba variable will be declared and set within the B4a class code (I think). It's definitely there and responding.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Yep, that works. Thanks

B4X:
anywheresoftware.b4a.keywords.Common.CallSubNew2(ba,this,"logresult",result);

I'm going to have to do some more digging :)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Raising an event from a class (this code will not work in other types of modules):

B4X:
Sub Class_Globals
   Private nativeMe As JavaObject
End Sub

Public Sub Initialize
   nativeMe = Me
   nativeMe.RunMethod("MethodThatRaiseEvent", Array("MyEvent"))
End Sub

Private Sub MyEvent_Fire(Value As Int)
   Log($"Fire: ${Value}"$)
End Sub

#If JAVA
import anywheresoftware.b4a.keywords.Common;
public void MethodThatRaiseEvent(String EventName) {
   Common.Log("use imports to make your code more readable!");
   ba.raiseEventFromUI(this, EventName.toLowerCase(BA.cul) + "_fire", 10);
}
#End If

1. Remember that the event string must be lower cased.
2. Use raiseEventFromUI instead of raiseEvent. Otherwise you will get a warning in rapid debug mode.
3. It is important that the event subs will be made of two parts separated with underscore. This is the convention in B4X and it will also work in obfuscated mode.
 
Upvote 0

Erwin Andersen

Member
Licensed User
Longtime User
Raising an event from a class (this code will not work in other types of modules):

B4X:
Sub Class_Globals
   Private nativeMe As JavaObject
End Sub

Public Sub Initialize
   nativeMe = Me
   nativeMe.RunMethod("MethodThatRaiseEvent", Array("MyEvent"))
End Sub

Private Sub MyEvent_Fire(Value As Int)
   Log($"Fire: ${Value}"$)
End Sub

#If JAVA
import anywheresoftware.b4a.keywords.Common;
public void MethodThatRaiseEvent(String EventName) {
   Common.Log("use imports to make your code more readable!");
   ba.raiseEventFromUI(this, EventName.toLowerCase(BA.cul) + "_fire", 10);
}
#End If

1. Remember that the event string must be lower cased.
2. Use raiseEventFromUI instead of raiseEvent. Otherwise you will get a warning in rapid debug mode.
3. It is important that the event subs will be made of two parts separated with underscore. This is the convention in B4X and it will also work in obfuscated mode.

when I try to compile, raise an error like this :

upload_2015-8-24_11-49-59.png
 
Upvote 0

Erwin Andersen

Member
Licensed User
Longtime User
Can you post your code?

The code shown below :

#Region Project Attributes
#ApplicationLabel: Inline Java
#VersionCode: 1
#VersionName: Test
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region

#Region Activity Attributes
#FullScreen: True
#IncludeTitle: True
#End Region

#Region Library Tambahan
#AdditionalJar : classes.jar
#AdditionalJar : gson-2.3.1.jar
#End Region

Sub Process_Globals
Private nativeMe As JavaObject
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
nativeMe.InitializeContext
End If

nativeMe.RunMethod("VTGetToken", Array("DataToken", False))
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub DataToken_Hasil(IsiToken As String)
Log(IsiToken)
End Sub

#If JAVA
import android.view.*;
import id.co.veritrans.android.api.VTDirect;
import id.co.veritrans.android.api.VTInterface.ITokenCallback;
import id.co.veritrans.android.api.VTModel.VTCardDetails;
import id.co.veritrans.android.api.VTModel.VTToken;
import id.co.veritrans.android.api.VTUtil.VTConfig;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;
import com.google.gson.Gson;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.objects.ViewWrapper;

public void VTGetToken(String EventName, boolean aman)
{
VTDirect vtDirect = new VTDirect();
VTConfig.VT_IsProduction = false;
VTConfig.CLIENT_KEY = "VT-client-mMP4410Ig1HRMb6l";
VTCardDetails cardDetails = null;

cardDetails = CardFactory(aman);
vtDirect.setCard_details(cardDetails);

vtDirect.getToken(new ITokenCallback()
{
public void onSuccess(VTToken token)
{
BA.Log(token.getToken_id());
BA.Log(token.getBank());
BA.Log(Integer.toString(token.getStatus_code()));
BA.Log(token.getStatus_message());
BA.Log(token.getRedirect_url());

ProsesBalikan(EventName, token.getToken_id());
}

public void onError(Exception e)
{
mymsgbox("Error", e.getMessage());
ProsesBalikan(EventName, "Error");
}
});

}

private void ProsesBalikan(String Nama, String IsiToken)
{
BA.Log("Isi data : " + IsiToken);
pBA.raiseEventFromUI(this, Nama.toLowerCase(BA.cul) + "_hasil", IsiToken);
}

private void mymsgbox(String str, String str2)
{
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setTitle(str);
dlgAlert.setInverseBackgroundForced(false);
dlgAlert.setMessage(str2);

dlgAlert.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
BA.Log("Tombol OK");
}
});

dlgAlert.setCancelable(true);
dlgAlert.create().show();
}

private VTCardDetails CardFactory(boolean secure)
{
VTCardDetails cardDetails = new VTCardDetails();
cardDetails.setCard_number("4811111111111114");
cardDetails.setCard_cvv("123");
cardDetails.setCard_exp_month(1);
cardDetails.setCard_exp_year(2020);
cardDetails.setSecure(secure);
cardDetails.setGross_amount("1000000");

return cardDetails;
}

#End IF
 
Upvote 0
Top