Search code examples
androidbackground-process

Run a process in background in Android


I'm developing an app that has to connect to a server once every hour. This happens without the user having to open the app.

  1. Can apps run in the background? Meaning if someone using my app is playing a game when my app has to connect to the server, can my app connect without interrupting the user playing their game. I know the I-Phone only allows one 3rd party application to run at a time, is IOS different?

  2. Assuming that apps can run as background processes, can they stay open for ever? I know that almost all of the newer phones don't actually give the user the ability to truly terminate a application and the system will actually terminate programs whenever it wants.

  3. If apps can't run in the background and stay open for ever, then can an app be automatically opened by the phone every hour?

I've been looking at alarm clock apps, as they seem to be a background process that open/ run at a set time, which is exactly what I want my app to do. Sadly I can't find any good source code. All the alarm clock examples require the app to be open or the alarm won't go off. Any help would be much appreciated. (please don't link me to the google source, I have already looked at that)


Solution

  • 1-> Yes your app can connect to the Server in Background without interrupting the user. You need to use a background Service to achieve this.

    2-> You can have a background service that keeps running but its really a bad idea , to keep it running for ever .This will drain the battery and use the memory ,cpu and network unnecessarily. System has control over the apps it can kill the background processes when required.

    3-> I don't think that there is a provision by which you can automatically open an app. Unless you are using some kind of Service that sends a broadcast or an event to app that will launch it. This is a bad design again. User will not like if his/her phone will behave weirdly :)