I have an android app with multiple activities. One needed class contains a socket connection server.
I am deciding to make this server class either as a nested sub class in the main activity or extend Service to run in the background as a service class.
Some people have commented that the service can really burn down the users battery and I have found that it is hard to kill a service class. I sometimes use a notification in the top menu so a user can stop the service and the app with a button click. but when the user does this it gives an error message that looks like the app crashed and that does not look kosher.
by putting the socket server class in a blank activity that contains no xml layout file it will probably bring up a blank page that will lose focus from the main activity, and that will not look good, unless there is some way to get around this problem.
one alternative idea is to put the socket server inside of a utility class that does not extend any other class and use a nested broadcast receiver or intents to send messages back to the main activity.
another alternative idea is the put the socket server inside of a class that extends application.
what is the correct way to implement this socket server class?
we have a (bluetooth) server socket that runs in a thread in a dedicated service with no issues reported by users.
In my opinion you should “Suck it and see” ie unless you try it and test it you’ll never know. Yes, there are alternatives and yes it will take more than zero additional battery, but test it and take measurements.