简体   繁体   中英

Android - constantly listening to UDP socket

My application based on constant client-server communication.

I'm trying to do that using service and alarmmanager.

I'm puting while(true) loop in onStartCommand() method in order to keep listening to specific port using DataGramSocket.

My problem is that it kiils my battery.

is there's any better way to implement constant UDP listening (without push notifications)?

Thank's

This is an old thread, but since I've stumbled upon it while looking answers to my own questions:

You haven't specified what you were doing inside your infinite loop.

In principle, simply calling DatagramSocket.receive() should be an efficient waiting on the socket. As long as the socket does not have any data in its incoming buffer the method should block without wasting any resources. Is this what you did? Can you post some code?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM