我怎样才能让我的应用程序在后台运行? [英] How can I keep my app running in the background?

查看:284
本文介绍了我怎样才能让我的应用程序在后台运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创造的,我需要始终通过TCP套接字连接的应用程序。我的应用程序已经可以很好的连接方面,但是当它被发送到后台,android系统最终杀死的过程。这使之成为与服务器断开连接。

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

编辑 这是我的code部分:

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.

推荐答案

使用服务,并通过 startService启动()。它将运行,直到机器人停止它的资源

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

在服务的一些文档:

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

这篇关于我怎样才能让我的应用程序在后台运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆