简体   繁体   中英

How can I keep my app running in the background?

I am creating an application in which I need to always be connected via a TCP socket. My application already works well in terms of the connection, but when it gets sent to the background, the android system eventually kills the process. This causes it to become disconnected from the server.

I've been looking for a way to always keep the application alive, but haven't found anything. Could someone tell me what would be the best way to make it so that my application is not closed when it's in the background, or, barring this, make it restart itself if it is closed? I'm starting with this and is producing me a headache :S

EDIT This is part of my code:

  public int onStartCommand(Intent intent, int flags, int startId) {

      Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();

      new Thread(new Runnable() { 
            public void run() {
                Playit();               }
        }).start();


     return START_STICKY;
  }

The app appears to be frozen on startup, though. I do not have a lot of experience, so maybe my mistake is simple and I haven't noticed it.

Use a Service and start it via startService() . It will run until Android stops it for resources

Some documentation on services:

  1. http://developer.android.com/reference/android/app/Service.html
  2. http://developer.android.com/guide/components/services.html

User a Service . Services are used to perform long-running operations in the background, like sending/receiving data in a connection with a server.

Look into Android's Service class. Note though, that a Service is NOT a separate thread, it is just a part of your application thread, it is just handled differently by the OS in terms of creating and destroying.

If you need an extra thread, you can of course create one in a Service .

The nature of OS is as such, that is it is supposed to kill background processes because of limited memory resources i guess . Maybe you should check out Watchdog its some kind of background process manager . I am not that used to the android system but this is what i could find from my research . Hope that helps :)

Maybe this will help you :

http://lifehacker.com/5608163/watchdog-monitors-your-android-for-run+away-processes

As the others said, the straight answer to you question is to host a thread that keeps the connection alive into a service.

However I want to remember you that you are in a resource limited environment, where having a persistent connection in background (with the overhead of the tcp protocol) may result into battery drain and angry users.

You should ask yourself if you really want to keep the connection persistent forever, or for a limited amount of time, or just after some events that might be triggered by a gcm message or if you could just go for polling, maybe using an inexact alarm .

Another option would be to establish the connection only when you application is in foreground. Anyway, it certainly depends on what you want to do with your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM