Android Question UDP thread

Hakanunver

Member
Licensed User
Hii
I want to receive data from UDP . But I get error "android.os.NetworkOnMainThreadException"
I want to use UDP on thread library.
1- This offical library I dont find
2- I need Example How I use UDP_PacketArrived on thread library

Thanks..
 

lemonisdead

Well-Known Member
Licensed User
Longtime User
Really, I am not an expert, so please don't take my reply as if it was 100% correct. It is a long time I didn't use any broadcasting method (and threads). But here is how I understood this: the threads are used to make some computation at the same time. When a thread has finished working, it returns to the Main thread (not the Main activity).
So, if I understand it correctly, your UDP receiver is to declare in the Main thread (perhaps in a Service if you want it to be ran all time). After it was initialized, when it receives some packets, the UDP_PacketArrived sub is called. Then, you can start a new thread to make something in the background. If I understand it, you don't have to create a special thread where to declare the UDP receiver.
I hope this can help, a bit
 
Upvote 0

Hakanunver

Member
Licensed User
1- I need Always Listen the UDP port
2 -when I receive data from udp I have "Android Policy " problem
3 - if I use " DisableStrictMode " freeze after 5 second.

How I fix This Problem ?

Thanks..
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
In short, my replies :
1. you should move the declaration into a service (the Starter service is a good place)
2. I bet you currently have declared in the Main Activity, if it is the case, moving the declaration to a service will solve
3. perhaps could you share some code, because for now, I see no reason. Is DisableStrictMode a method related to the UDPSocket ?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Is DisableStrictMode a method related to
It is a method related to android apps. If you do network-connections in the main-thread then you run into an network on main thread exception.
DisableStrictMode allows it. But it is not suggested to do network on the main-thread.

I would write a java lib to do the communication and use threads. After the thread is fnished i would raise an event in b4a.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
 
Upvote 0

Hakanunver

Member
Licensed User
Hii #DonManfred what you think possible use udp communication asyncstreams or AsyncStreamText ?
Because I need udp. I need listen brodcast messages..
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…