How to keep IM thread running on android? - android

I am building an android IM chat app which needs to keep a long connection to the server. However, on some android phones (not all android phones), when a user locks their phone the app and its thread (running in Service) are paused and the chat app can no longer receive messages.
I want to know how to solve the problem?

You should use a Service. There is never a guarantee that it will always stay running, but unlike activities, services are designed to run in the background. They're more likely to stay running if you request foreground status.
You can find more information about services here.

Related

What are the conditions for a Foreground service to run indefinitely in Android 8?

My main app supports different hardware attachments. It ranges from RFID readers to stepper motors controllers.
Because the app is quite independent and the scenarios where attachments are used is rare, I have decided to remove all the code from the main app and move it to services which are installed on specific devices.
The services used to communicate using the Intents.
Recently the devices running the app were upgraded to Android 8 and I can no longer use a background service as I used to because it's killed after a few moments.
I went through the docs in here: https://developer.android.com/guide/background
It seems I should use the Foreground Service, however when searching for more info about foreground services I have found that it is not a suitable solution for long-running processes (the RFID reading process runs from boot to shutdown).
When developing the first solution a while back I have decided not to use the Bound services as they are far less convenient than just waiting for the incoming intents.
I don't mind at all the notification icon, I actually like it as it will allow us to easily see what's running and what's not when interacting with a device.
I just don't want to invest time in rebuilding the app and then ending up with something that does not work.
What should I do to make sure that the service will run indefinitely and will not be killed? I will not be able to monitor it from my main app and restart when needed.

persistent network connection in an android service

I'm developing a similar app to Telegram or WhatsApp, I need to keep a persistent socket connection to a server (I know this is a resource intensive job and I am sure GCM is not going to help in this case, I can't change the architecture at the moment).
First of all I tried writing a Service but since the service is running in the same process as the app's main thread, whenever the app is closed, then the service is restarted. (I have returned START_STICKY on onStartCommand()).
After that I am begining to think I would have to develope an independent service and use AIDL to interact with my app.
I'd appreciate if you could help me with this problem.
Users can always kill your app if they want to. If you've marked your Service as STICKY, Android will simply restart it for you. In this case you will need to recognize that your app has been restarted and recreate the persistent socket connection. There is nothing you can do to prevent a user (or Android) from killing your app under certain conditions.

websocket connection keeps disconnecting [Android]

My app makes a websocket connection to a URL using this library. Everything is working fine. The app is a communication app (sends and receives messages). When the app goes to the background (pressing the home key) the websocket connection still works and the user receives push notifications for new messages.
The problem is that, when the app is in background, after some amount of time, the websocket connection disconnects automatically. Now this time interval is different each time (sometimes 5 seconds, sometimes 5 minutes).
Now the problem is not with the URL (no idle time/timeout issues - believe me, it works fine on other platforms). So I am looking for possible reasons for this behaviour so I can fix the problem.
How could the websocket connection be disconnected when the app goes in the background. Also, should I run a continuous background service as a remedy?
PS: There is no service in the app right now. The library is supposed to let the websocket connection running when the app goes to the background.
The issue is that the library you are using does not stop the connection when the app is backgrounded... however, Android may kill the process.
A Service exists specifically to prevent that problem. If you implement it in a Service it should solve your problem.
A word of caution: just because you can do this it does not mean that you should do this. Be careful about memory and battery usage when you implement a Service. They are intended to run in the background indefinitely - and you can make some users very unhappy if you have CPU/battery intensive code, or Android may kill the Service if it consumes too much memory.

prevent app to clear application data while running in background

I am developing an android application related to gps. that is sending location information periodically to the server. sometime i am getting process killing issue in some devices.
if my application running in foreground then its working file. but while we minimize app and move it to back or once phone going to sleep then app automatically clearing all application data and variables. and then when we tried to resume activity back, it showing blank information and generating exception.
how to prevent clear app data while app running in backgtound? i want to keep this information as it is. this issue only arise in some devices not all.
i have also tried research on Google, but not getting any good solution.
Please Help me. Thank you.
You can't do it like that. If you want your application to keep sending data to a remote service even if the app is in the background, you need to do it a long running Service. Keep in mind that a service runs by default in the main thread, so you need to run your comms with the server in a background thread (however you like it, asynctask, wtv). Then, to really make sure Android doesn't kill your service if it's running low on memory, you need to set your service as a foreground service with a notification.
That way, even if your app is sent to the background you're sure Android the communication will continue.

Which background model to run

I am trying to develop an application which will require a service to
run in the background. I am relatively new to android programming,
and after reading many posts, blogs, how-to's and books on creating
and managing services, I am still pretty confused about which model I
should try to use.
First, let me present (in general) the application requirements: I
need an application which will spawn a background process (service?)
which will connect to a bluetooth device. The bluetooth device is
designed to deliver data to the android device. The issue is that the
data could come in at any moment, so the bluetooth connection has to
stay active. Note that the application is a VERY SPECIFIC app and is
NOT intended for public use. I do understand the arguments for not
having background apps running all the time, but please understand
that this is a very specific application for a very specific client.
Now, in general, I think the program flow would be to start the
application (and launch a UI activity). Then I need to configure and
connect to the bluetooth device. At this point, the user should be
able to do other things - make phone calls, check their email, etc.,
while the bluetooth connection is still active and potentially
receiving data. If data comes in, a notification is fired, etc.
So here are my questions and concerns:
If I start an app (which spawns a UI activity and ultimately my
bluetooth connection service) but the app is killed, apparently, the
service handling the bluetooth connection is killed as well. How can
I keep that alive? I read that Service.setForeground() was
depricated, but even if I were to set it to the foreground, if the app
is killed, the service is killed as well. I need to have it run in
the background with as high of a priority as possible (again, I do
understand that this is considered "bad form", but this is a specific
app and this functionality has been requested by the client).
If I started the app (and the service, etc.), but the user, say,
answers a phone call, the app is put into the background. However,
let's say the user goes back to the home screen and starts a DIFFERENT
instance of the app, i.e., he doesn't hold down the home key to select
the already running app from the task manager but starts a completely
new one. If the service handling the bluetooth connection is still
running, how will this new instance behave? i.e., how can I get it to
connect to the bluetooth service which is ALREADY running in the FIRST
instance of the app instead of this new instance? Do I have to use
some form of a Remote service instead of a local service? This is
where I'm a little confused by things as it seems remote services and
defining an AIDL seems to create a lot of extra overhead, and since
I'm already creating a lot of overhead with the service running in the
background all the time, I want to keep that as small as possible.
How can I insure I am connecting to the same service already running?
1)
The service does not depend on an Activity. You can have it running on the background until you call stopSelf().
You can have a BroadcastReceiver that listens to the android.intent.action.BOOT_COMPLETED so your service is started when the phone is turned on.
2)
Your Activity should bind to the service. And get the info from it.
Check this question.

Categories

Resources