Android套接字和异步任务 [英] Android sockets and asynctask

查看:56
本文介绍了Android套接字和异步任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将要开始制作一个应作为tcp聊天客户端的应用程序,并且我一直在阅读和阅读,我得出的结论是最好放(如果不需要)?我在异步任务中的套接字和读取器.

I'm about to start making an app that's supposed to act as a tcp chat client and I've been reading and reading and I've come to the conclusion that it would be best (if not required?) to put my socket and reader in asynctask.

我不确定从哪里开始,因为我是android的新手,至少对我来说这是一项艰巨的任务,但就我所了解的基础而言.

Thing is I'm not sure on where to begin, as I'm new to android this is a massive undertaking for me at least but the basics as far as I've understood it.

  1. 实例化主类中的视图,并创建一种用于将文本从EditText发送到异步类的方法.

  1. Instantiate the views in the mainclass and create a method for sending text from a EditText to the async class.

使用套接字和连接创建异步类,将读取器和写入器放在此处,写入器将是接收通过套接字发送的字符串参数的方法.

Create the async class, with the socket and the connection, place the reader here and the writer, the writer will be a method to take in a string parameter which is sent through the socket.

以某种方式使我的读者不断聆听服务器套接字,并使用发出的字符串更新我的textview.

Somehow make my reader constantly listen to the server socket and update my textview with strings that are sent out.

这是开始进行编程的一个很好的基本计划",还是某种程度上的缺陷,或者我错过了什么?我什至在开始之前就遇到的一个问题是异步任务,我做到了吗?据我所知,我不需要返回任何东西,也不需要任何更新进度吗?我为客户提供的地址将经过硬编码,所以我猜那是我唯一想向异步类传递参数的东西吗?

Is this a good basic "plan" to start programming for or is it flawed somehow or me missing something? One question that I got before even starting is the async task, do I make it ? As far as I'm aware I don't need to return anything and I won't be needing any progress updating? My address for the client will be hardcoded so I'm guessing that's the only thing I would want to pass a parameter to the async class?

很抱歉,如果它是基本的,但我才刚刚开始,并且希望不要从错误开始.

Sorry if its basic but I'm just starting and would like to not start off wrong.

推荐答案

您只想将AsyncTask用于较小的(相对)直接操作,例如下载特定项目(例如图像或mp3).听起来您想要的是服务.该服务将在您的应用程序的后台运行,您可以通过该服务处理网络操作.

You only want to use an AsyncTask for small (relatively) and direct actions, such as downloading specific items (eg. Images or mp3's). It sounds like what you want is, instead, a Service. The service will run in the background of your application and you can handle your network actions through the service.

要获取其他有用的文档和帮助,以查看服务是否适合您,看这里.

For other helpful documentation and help on seeing if the Service is right for you, look here.

有关在其自己的线程中创建服务的更多信息,请请参阅此.

For more information of creating a Service in its own Thread, refer to this.

尝试将其视为反活动.它完成了活动不应该做的所有事情,主要是处理.出于多种原因,任何硬核处理都应在线程服务中完成,主要是因为多任务处理和将处理从主UI线程中删除.为了说明这一点,请考虑Pandora. Pandora创建了一个服务(在其自己的线程中),该服务连接到其站点并传输音乐.该服务具有生命周期,可以在活动被杀死的同时生存.这允许音乐流将用户执行其他任务.但是,当用户想要查看正在播放的内容时,只需点击即可进入该应用程序.活动将从那里检查服务,查看它是否包含一些数据,并根据服务中的数据来确定其内容.

Try to think of is as the anti-Activity. It does everything that an activity should not have to do, primarily, processing. Any hardcore processing should be done in a threaded service for many reasons, primarily for multi-tasking and taking processing off the main UI thread. To demostrate this, think of Pandora. Pandora creates a service (in its own thread) that connects to its site and streams music. The service has a life cycle that will allow it to live while the activities are killed. This allows the music to stream will the user does other tasks. But when the user wants to look at what is playing, she will only have to click into the app. From there the activity will check the service, see that it has some data, and base its content off of the data in the service.

我希望这会有所帮助.我真的想不出更好的理由...

I hope this helps. I really can't think of a better why to put it....

这篇关于Android套接字和异步任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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