如何保持通过活动服务器套接字? [英] How to maintain Server Socket through Activities?

查看:132
本文介绍了如何保持通过活动服务器套接字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我决定做一个服务器和客户端通过套接字(只是测试我的想法现在)。

In my application I decided to make a server and client through Sockets (just testing my idea for now).

所以我做了两个 AsyncTasks (服务器和客户端)的在我的onCreate(取决于用户操作 - 正如我所说,我只是测试,现在只是按钮):

So I made two AsyncTasks (for the server and client) an on my onCreate (depending on user action - as I said I'm just testing, for now just with buttons):

new createServer().execute(); 

new connectClient().execute();

问题是,一切都运行良好,但在这里我要实现我的想法的应用程序有许多活动,让正在服刑的数据应用,当离开活动,其中createServer异步任务是,客户端(显然)失去连接。

Question is, everything works good, but the app where I want to implement my idea has many activities, so the app that is serving the data, when leave the activity where the createServer Async Task is, the client (obviously) loses the connection.

有维持运行所有时间的服务器,并允许用户(与应用程序服务器)通过应用程序导航,而不会影响连接的客户端的方法吗?因为即使每次我离开一个活动,我创建了服务器,客户机将失去反正连接。

Is there a way to maintain the server running "all time", and allow the user (with the "server app") to navigate through the app without affecting the connected clients? Because even if everytime I leave an activity I create the Server, the clients will lose connection anyway.

真的AP preciate任何建议。

Really appreciate any suggestion.

编辑:对于我的测试应用程序的目的,我需要对系统进行的工作只是在前台,没有背景,所以没必要的服务创造

For the purposes of my test application, I need the system to be working only in foreground, no background, so no need of service creation.

推荐答案

您的问题是与正确管理您的服务器的生命周期。首先你必须决定你想这一辈子是什么。显然,你已经想通了,你希望它是不仅仅是活动水平范围较广,但你希望它在后台运行的程序是运行在前台还是只?如果只是在前台的时候,你可以逃脱不创建一个服务,而是从应用程序中的对象保持引用到组件。不过,如果你想保持你的服务器上运行,即使您的应用程序是你需要服务的背景。

Your problem is with correctly managing the lifetime of your server. First you'll have to decide what you want that lifetime to be. Clearly you've figured out that you want it to be broader than just the activity level, but do you want it to run in the background or only when your app is in the foreground? If it is only when in the foreground, you can get away with not creating a service, but instead keeping a reference to a component from your application object. However, if you want to keep your server running even when your app is in the background you do need a Service.

更新:

这里是我的意思是从你的应用程序对象保持到组件的引用的例子:

Here's an example of what I meant by "keeping a reference to a component from your application object":


  • 创建一个自定义的应用程序类。

  • 添加一个字段来保存到您的服务器的AsyncTask参考

  • Create a custom Application class.
  • Add a field to hold a reference to your server AsyncTask

private AsyncTask mServerTask;


  • 一个方法添加到它来启动服务器的AsyncTask

  • Add a method to it to start your server AsyncTask

     public void startServer() {
         mServerTask = new createServer();
         mServerTask.execute();
     }
    


  • 可能添加另一种方法来停止服务器

  • Probably add another method to stop your server

    要从一个活动,您现在可以做这样的事情启动服务器:

    To start your server from an activity you can now do something like:

    CustomApplication app = (CustomApplication) getApplication();
    app.startServer();
    

    当然,你需要做更多的妥善管理就像确保你不启动它的两倍,等等,你可能决定使用线程,而不是AsyncTasks服务器,但这应该给你的,你怎么一个想法可以有超出活动一辈子的对象。

    Of course you'll want to do more to properly manage the server like making sure you don't start it twice, etc. and you might decide to use threads instead of AsyncTasks but this should give you an idea of how you can have an object with a lifetime beyond that of an activity.

    这篇关于如何保持通过活动服务器套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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