Android Question Translating an old app to B4XPages: Notify function problem

GuyBooth

Active Member
Licensed User
Longtime User
In my old app I used the following statement:
B4X:
nNotify.SetInfo("The Music Machine", Message, B4XPTMM_Run)
where TMM_Run was an activity.
With B4XPages it is now a B4XPages class. If I translate the code to:
B4X:
nNotify.SetInfo("The Music Machine", Message, B4XPages.MainPage.TMM_Run)
the logs give me the following error:
B4X:
Process object is expected. {Type=B4XPTMM_Run,Rank=0, RemoteObject=True} is an Activity object.
Declaring the variable in Sub Globals instead of Sub Process_Globals will solve this problem.
I'm not aware that Sub Globals is valid in a B4XPages class. The format for the nNotify is
B4X:
n.SetInfo(Title as Charsequence, Text as CharSequence, Activity as Object)
Obviously the B4XPage class is not an activity. What should the notify method be?
 
Solution
You need to pass an Activity to SetInfo:
B4X:
nNotify.SetInfo("The Music Machine", Message, Main)
Main is the activity that hosts all B4XPages.

GuyBooth

Active Member
Licensed User
Longtime User
B4X:
nNotify.SetInfo("The Music Machine", Message, B4XPages.GetNativeParent(Me)) 'assuming that is called from inside the page
It isn't being called from inside the page - it's being called from a service, so the above doesn't work....
this makes the log errors go away (B4XPTMM_Run is the B4XPage ID):
B4X:
nNotify.SetInfo("The Music Machine", Message, B4XPages.GetPage("B4XPTMM_Run"))
 
Last edited:
Upvote 0

GuyBooth

Active Member
Licensed User
Longtime User
While my change to this code made the error go away, my notification setup code crashes at that line.
I'm running the code from the service I am starting up called "TMM_FlipFlop". This is the code:

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    ....
    Private NotifyString, Notifier As String
    ....
End Sub
Sub Service_Create
    ....
    SetNotification("Nothing Loaded.")
    Service.StartForeground(1,nNotify)
    ....
End Sub
' Set the notification to carry the passed in message
Sub SetNotification(Message As String)
    nNotify.Initialize
    nNotify.Icon = "icon"
    nNotify.Sound=False
    nNotify.Vibrate=False
    nNotify.Light=False
    nNotify.OnGoingEvent=True
    nNotify.SetInfo("The Music Machine", Message, B4XPages.GetPage("B4XPTMM_RunID"))
    nNotify.Notify(1)
End Sub
When it runs the program crashes at the nNotify.SetInfo line with these errors:
B4X:
*** Service (tmm_flipflop) Create ***
Error occurred on line: 404 (B4XPagesManager)
java.lang.ClassCastException: b4a.TMMX.b4xptmm_run cannot be cast to android.content.Intent
    at anywheresoftware.b4a.keywords.Common.getComponentIntent(Common.java:989)
    at anywheresoftware.b4a.objects.NotificationWrapper.SetInfo2New(NotificationWrapper.java:202)
    at anywheresoftware.b4a.objects.NotificationWrapper.SetInfoNew(NotificationWrapper.java:182)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
... etc
I assume from this that I am not understanding the syntax. I have tried variations of it with no success.
 
Upvote 0
Top