简体   繁体   English

如何让我的应用程序在后台运行?

[英]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. 我正在创建一个应用程序,我需要始终通过TCP套接字连接。 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. 我的应用程序已经在连接方面运行良好,但是当它被发送到后台时,android系统最终会杀死进程。 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 我从这开始就让我头疼: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() . 使用服务并通过startService()启动它。 It will run until Android stops it for resources 它将一直运行,直到Android停止它的资源

Some documentation on services: 一些有关服务的文件:

  1. http://developer.android.com/reference/android/app/Service.html http://developer.android.com/reference/android/app/Service.html
  2. http://developer.android.com/guide/components/services.html 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. 查看Android的Service类。 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. 但请注意, Service不是一个单独的线程,它只是应用程序线程的一部分,操作系统在创建和销毁方面的处理方式不同。

If you need an extra thread, you can of course create one in a Service . 如果您需要额外的线程,您当然可以在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 . 也许你应该看看Watchdog它的某种后台进程管理器。 I am not that used to the android system but this is what i could find from my research . 我不习惯Android系统,但这是我可以从我的研究中找到的。 Hope that helps :) 希望有帮助:)

Maybe this will help you : 也许这会对你有所帮助:

http://lifehacker.com/5608163/watchdog-monitors-your-android-for-run+away-processes 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. 但是我想要记住你在资源有限的环境中,在后台持有连接(使用tcp协议的开销)可能导致电池耗尽和愤怒的用户。

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 . 您应该问自己,您是否真的希望永久保持连接,或者在有限的时间内保持连接,或者只是在某些可能由gcm消息触发的事件之后,或者您可能只是进行轮询,可能使用不准确的警报

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. 无论如何,它当然取决于你想要对你的应用程序做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用后台运行的应用程序使屏幕保持打开状态? - How can I keep my screen on using a background running app? 如何使用libGDX使我的应用程序在iOS / Android上在后台运行 - How can I keep my app running in the background on iOS/Android with libGDX 如何让Android应用程序在后台运行 - How can I keep an android app running in background 如何保持应用程序在后台运行? - How to keep app running in background? 关闭时如何让我的 flutter 应用程序在后台运行? - How to keep my flutter app running in the background when close? 当我杀死自己的应用程序时,如何确保后台服务不会继续运行? - How do I make sure that my background services don't keep running when I kill my own app? 即使我的应用程序被杀死,如何保持我的定位服务运行 - How Can I Keep My Location Service Running Even When My App Is Killed 当我在 android 应用程序中使用底部导航导航到不同的片段时,如何保持计时器运行? - How can I keep the timer running when I navigate to different fragment with bottom navigation in my android app? Android开发:当我的应用程序处于后台时,如何保持屏幕显示状态? - Android Development: How can I keep screen on when the activity of my app is at background? 使应用程序在后台运行 - Keep app running in background
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM