Android Question From activity to service

engvidal

Member
Licensed User
Longtime User
Hi

I have a very simple app that when opened connects to an mqtt broker, and each data received is appended to a multiline editText.

It runs ok, but..

If I go to sleep or other app, when I return, the mqtt connections was lost and no new data received.

How can I convert it to a service, so it continues receiving data and appending to edittext ?
 

Peter Simpson

Expert
Licensed User
Longtime User
'appended to a multiline editText', what sort of data is it, why EditText need more information. In a lot of cases you can just copy most of the code over and with a few tweaks it will work.

What sort of data are you receiving and sending?
 
Upvote 0

engvidal

Member
Licensed User
Longtime User
Hi.

If simplicity is the key what matters the data and usage ?

I just want that my app continues receiving data while in background, and do not "clear" the edittext when returns to the main activity, just like message text, whatsapp, and others.
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Hi.

If simplicity is the key what matters the data and usage ?

I just want that my app continues receiving data while in background, and do not "clear" the edittext when returns to the main activity, just like message text, whatsapp, and others.
If you use a service you won't be able to update that EditText, what you should do, is keep capturing that data (using the service) and save it to a file, when the Activity kicks in, populate the EditText with the file content.

You will have to modify your code to handle that accordingly.
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
If simplicity is the key what matters the data and usage ?
Just having that extra bit of information got you an answer.

Anytime the activity resumes, that's when you have to read the file thus updating the EditText. You may want to look into sticky service or better still foreground service.
 
Upvote 0
Top