Background running and listener.

rbsoft

Active Member
Licensed User
Longtime User
It depends what it is supposed to listen for.

Basically this would mean you have to create an app with a service. The service could be either running in foreground or check periodically for events.

Rolf
 
Upvote 0

winnunez

New Member
Here is the main activity:
B4X:
'Activity module
Sub Process_Globals
   
   
End Sub

Sub Globals
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("layout1.bal")
     StartService(Listener)
   
End Sub

Sub Button1_click
   SetMobileDataEnabled(True)
   
End Sub

Sub Button2_Click
   SetMobileDataEnabled(False)
   
End Sub

Sub Button3_Click
   Listener.Obj1.Target = Listener.Obj1.GetContext
   Listener.Obj1.Target = Listener.Obj1.RunMethod2("getSystemService", "phone", "java.lang.String")
   Listener.Obj1.Target = Listener.Obj1.RunMethod("getCellLocation")
   Listener.cid = Listener.Obj1.RunMethod("getCid")
   Msgbox(Listener.cid, "Hello this is your CellID")
   
   
End Sub

Sub SetMobileDataEnabled(enabled As Boolean)
   Dim r As Reflector
    r.Target = r.GetContext
    r.Target = r.RunMethod2("getSystemService", "connectivity", "java.lang.String")
    r.RunMethod2("setMobileDataEnabled", enabled, "java.lang.boolean")

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

This is the service file:
B4X:
'Service module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim fem As Int
   fem = 222822402
   Dim Obj1 As Reflector
   Dim cid As Int
   Dim Timer1 As Timer
      Timer1.Initialize("Timer1",10000)
End Sub

Sub Service_Create
   Timer1.Enabled=True
   
End Sub

Sub Service_Start 
      
End Sub

Sub Timer1_Tick
   Timer1.Enabled=True
   Obj1.Target = Obj1.GetContext
   Obj1.Target = Obj1.RunMethod2("getSystemService", "phone", "java.lang.String")
   Obj1.Target = Obj1.RunMethod("getCellLocation")
   cid = Obj1.RunMethod("getCid")
   ToastMessageShow("Hello", True)
   If cid = fem Then 
   SetMobileDataEnabled(True)
   Else   
   SetMobileDataEnabled(False)
   End If
   Timer1.Enabled=True


End Sub


Sub SetMobileDataEnabled(enabled As Boolean)
   Dim r As Reflector
    r.Target = r.GetContext
    r.Target = r.RunMethod2("getSystemService", "connectivity", "java.lang.String")
    r.RunMethod2("setMobileDataEnabled", enabled, "java.lang.boolean")

End Sub

Sub Service_Destroy

End Sub

What happens is after 10 seconds when I open the app is that a popup will show saying that the app has stopped working and then close.
 
Upvote 0
Top