Hello Forum!
I am implementing a broadcast receiver in my app for detecting when connection is available on the device.
I use this code in my app:
If i use this code in Starter service it seems to work on some devices but on some others it seesm it crashes with something like:
And also someone else said that broadcast receivers should not be put in starter service (https://www.b4x.com/android/forum/threads/broadcast-receiver-not-working.95679/)
So i put the broadcast receiver in another service but now i am experiencing another problem: it seems that Android 8 kills background services so when the connectivity changes the receiver doesn't receive nothing.
Any suggestion about detecting connection available?
(I need this because i created a chat app and when i send a messge while there is no connection it is put in a queue and the app waits until network connection is detected to retry sending the message)
I am implementing a broadcast receiver in my app for detecting when connection is available on the device.
I use this code in my app:
B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim BroadCast As BroadCastReceiver
End Sub
Sub Service_Create
BroadCast.Initialize("BroadcastReceiver")
BroadCast.addAction("android.net.conn.CONNECTIVITY_CHANGE")
BroadCast.SetPriority(2147483647)
BroadCast.registerReceiver("")
End Sub
Sub BroadcastReceiver_OnReceive (Action As String, i As Object) 'https://www.b4x.com/android/forum/threads/broadcastreceiver.12493/
Dim retIn As Intent
retIn = i
Dim IsConnected As String
'Dim TheType As String
If Action = "android.net.conn.CONNECTIVITY_CHANGE" Then
Dim jo As JavaObject = retIn
Dim NetworkInfo As JavaObject = jo.RunMethod("getParcelableExtra", Array("networkInfo"))
IsConnected = NetworkInfo.RunMethod("getState", Null)
If IsConnected.EqualsIgnoreCase("CONNECTED") Then
'Do this...
Else
'Do that...
End If
End If
End Sub
Sub Service_Start (StartingIntent As Intent)
Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
End Sub
Sub Service_Destroy
End Sub
If i use this code in Starter service it seems to work on some devices but on some others it seesm it crashes with something like:
B4X:
java.lang.RuntimeException:
at anywheresoftware.b4a.keywords.Common$11.run (Common.java:1167)
at android.os.Handler.handleCallback (Handler.java:789)
at android.os.Handler.dispatchMessage (Handler.java:98)
at android.os.Looper.loop (Looper.java:164)
at android.app.ActivityThread.main (ActivityThread.java:6944)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:327)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1374)
Caused by: java.lang.IllegalStateException:
at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1538)
at android.app.ContextImpl.startService (ContextImpl.java:1484)
at android.content.ContextWrapper.startService (ContextWrapper.java:663)
at anywheresoftware.b4a.keywords.Common.StartService (Common.java:894)
at anywheresoftware.b4a.keywords.Common$11.run (Common.java:1137)
And also someone else said that broadcast receivers should not be put in starter service (https://www.b4x.com/android/forum/threads/broadcast-receiver-not-working.95679/)
So i put the broadcast receiver in another service but now i am experiencing another problem: it seems that Android 8 kills background services so when the connectivity changes the receiver doesn't receive nothing.
Any suggestion about detecting connection available?
(I need this because i created a chat app and when i send a messge while there is no connection it is put in a queue and the app waits until network connection is detected to retry sending the message)