Android Question Network library work only debug mode

thinkinvention

New Member
Licensed User
I try to use network library with UDP. The sample UDP so simple but it work only debug mode. How to work in release mode ?

https://www.b4x.com/android/help/network.html

Sub process_globals
Dim UDPSocket1 As UDPSocket
End Sub

Sub Globals

End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
UDPSocket1.Initialize("UDP", 0, 8000)
End If
Dim Packet As UDPPacket
Dim data() As Byte
data = "Hello from Android".GetBytes("UTF8")
Packet.Initialize(data, "10.0.0.1", 5000)
UDPSocket1.Send(Packet)
End Sub
Sub UDP_PacketArrived (Packet As UDPPacket)
Dim msg As String
msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
Msgbox("Message received: " & msg, "")
End Sub

I got error like this

Logger connected to: ce0416045d4f580305
--------- beginning of main
--------- beginning of system
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
main_activity_create (java line: 358)
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1448)
at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:302)
at libcore.io.IoBridge.sendto(IoBridge.java:569)
at java.net.PlainDatagramSocketImpl.send(PlainDatagramSocketImpl.java:124)
at java.net.DatagramSocket.send(DatagramSocket.java:721)
at anywheresoftware.b4a.objects.SocketWrapper$UDPSocket.Send(SocketWrapper.java:289)
at b42a.example.main._activity_create(main.java:358)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:191)
at b42a.example.main.afterFirstLayout(main.java:104)
at b42a.example.main.access$000(main.java:17)
at b42a.example.main$WaitForLayout.run(main.java:82)
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)
 

MarkusR

Well-Known Member
Licensed User
"NetworkOnMainThreadException"
its not allowed to block the main thread in any way.
i guess u must put the network code into a service.
 

thinkinvention

New Member
Licensed User
"NetworkOnMainThreadException"
its not allowed to block the main thread in any way.
i guess u must put the network code into a service.
I try to put network code into a service module but still error like this

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim UDPSocket1 As UDPSocket
End Sub
Sub Service_Create
UDPSocket1.Initialize("UDP", 0, 8000)

End Sub
Sub Service_Start (StartingIntent As Intent)
Dim Packet As UDPPacket
Dim data() As Byte
data = "Hello from Android".GetBytes("UTF8")
Packet.Initialize(data, "10.0.0.1", 5000)
UDPSocket1.Send(Packet)
Log("udp sended")
' Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
End Sub
Sub Service_Destroy
End Sub
Sub UDP_PacketArrived (Packet As UDPPacket)
Dim msg As String
msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
Msgbox("Message received: " & msg, "")
End Sub

The error meassage

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Resume **
*** Service (testservice) Create ***
** Service (testservice) Start **
testservice_service_start (java line: 164)
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1448)
at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:302)
at libcore.io.IoBridge.sendto(IoBridge.java:569)
at java.net.PlainDatagramSocketImpl.send(PlainDatagramSocketImpl.java:124)
at java.net.DatagramSocket.send(DatagramSocket.java:721)
at anywheresoftware.b4a.objects.SocketWrapper$UDPSocket.Send(SocketWrapper.java:289)
at b42a.example.testservice._service_start(testservice.java:164)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:191)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
at b42a.example.testservice.handleStart(testservice.java:100)
at b42a.example.testservice.access$000(testservice.java:8)
at b42a.example.testservice$1.run(testservice.java:71)
at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.onStartCommand(ServiceHelper.java:221)
at b42a.example.testservice.onStartCommand(testservice.java:69)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3679)
at android.app.ActivityThread.-wrap21(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1801)
at android.os.Handler.dispatchMessage(Handler.java:105)
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)
 
Top