0

I am new to Android and I am implementing one chat application. I want to create a service which will send one message each 5 seconds to the chat so I can test receiving messages.

I am not sure which service should I implement. I am reading about binded and started, and about Service and IntentService classes. Should I make a separate thread where I could sleep for 5 seconds and run in a loop the same sending message work? I understood that I should in onCreate() method initialize things for sending messages, but I am not sure if I should implement loop in onStartCommand() or somewhere else? I want the service to send always messages so started service should be choosen,right?

Can someone tell me a little bit more about what should I use and why.

Thanks in advance

newstudent
  • 65
  • 10
  • 1
    Perform a network task every 5 seconds? Forget about it. First of, its a very very bad practise. Second, at max you'd be able to go around 15 minutes with `WorkManager` & that too isn't exact. AlarmManager maybe okay for 'exactness' but never for this type of task. Atlast, you can use a `Foreground Service`. – Darshan Aug 23 '21 at 15:18
  • @DarShan it is not network, it is sending a message to database. 5 seconds is for testing... to make like someone is really chatting and sending messages so I need to respond – newstudent Aug 23 '21 at 15:29
  • If you want to make it look as if someone is chatting, add items to your `RecyclerView` or relevant `View` after every X seconds, adding items to a database is also an expensive operation even if 5 seconds is for testing. – Darshan Aug 23 '21 at 15:34
  • @DarShan I am using chat sdk so I have there database and I want those messages to be saved when I open the app again – newstudent Aug 23 '21 at 15:42

0 Answers0