Android - httpclient 作为后台服务 [英] Android - httpclient as a backgroundservice

查看:26
本文介绍了Android - httpclient 作为后台服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序可以登录网络服务并上传文件.当我进入不同的屏幕并从网络服务获取数据时,我需要保持会话处于活动状态.我读到我需要将 http 调用作为服务进行,并且可能会使用该服务启动我的应用程序.我如何将我的登录"活动和上传"活动 httpclient 调用放入 http 服务活动中?

i have an app that login to a webservice and also uploads file. I need to keep the session alive as i go to different screens and get data from webservice. I read i need to make the http calls as a service and maybe boot my app with the service. How do i put my "login" activity and "upload" activity httpclient calls inside a http service activity?

谢谢.

推荐答案

由于服务与 UI 线程在同一线程上运行,因此您需要在不同的线程中运行该服务.您可以通过多种不同的方式执行此操作:

Since a service runs on the same thread as the UI thread, you will need to run the service in a different thread. You can do this in several different ways:

  1. 在服务的onCreate()onBind() 等方法中使用常规Java 线程
  2. onCreate() 方法中使用 AsyncTask - 另一种形式的线程,但如果您需要进行 UI 更新则更简洁
  3. 使用提供异步服务任务执行的 IntentService - 不确定它的效果如何,因为我从未使用过它.
  1. Use regular java threading within the service's onCreate () or onBind() etc, methods
  2. Use AsyncTask within the onCreate() method - another form of threading but much cleaner if you need to do UI updates
  3. Use IntentService which provides asynchronous Service task execution - not sure how well this works as I have never used it.

所有这三种方法都应该允许您在后台和通过服务与 HttpClient 建立连接,尽管我从未使用过 IntentService,但它对我来说似乎是最好的选择.如果您需要更改只能在 UI 线程上完成的 UI,AsyncTask 将非常有用.

All three of these methods should allow you to make connections with the HttpClient in the background and through a Service and even though I have never used IntentService, it looks like the best option to me. AsyncTask would be very useful if you need to make changes to the UI which can only be done on the UI thread.

按请求所以我目前正在做一些需要以异步方式进行 Http 连接的事情.发表这篇文章后,我尝试做第 3 条,它确实很好/很容易地工作.唯一的问题是信息必须通过意图在两个上下文之间传递,这真的很难看.因此,这里有一个近似示例,说明您可以在异步后台服务中建立 http 连接.

Edit by request: So I am currently doing something which requires Http connections in a asynchronous way. After making this post, I tried doing number 3 and it does work very well/easily. The only problem is that information has to be passed between two contexts through intents which is really ugly. So here is an approximate example of something you can do to make http connections in an asynchronous, background, service.

从外部活动启动异步服务.我放了两个按钮,以便在服务运行时可以看到活动正在执行.Intent 可以在您想要的任何地方启动.

Launch the asynchronous service from an outside activity. I put two buttons just so the activity can be seen executing while the service is running. The intent can be launched really anywhere you feel like.

/* Can be executed when button is clicked, activity is launched, etc.
   Here I launch it from a OnClickListener of a button. Not really relevant to our interests.                       */
public void onClick(View v) {
        Intent i = new Intent ("com.test.services.BackgroundConnectionService");
        v.getContext().startService(i);         
    }

然后在 BackgroundConnectionService 中,您必须扩展 IntentService 类并在 onHandleIntent(Intent intent) 方法中实现所有 http 调用.就像这个例子一样简单:

Then within the BackgroundConnectionService you have to extend the IntentService class and implement all http calls within the onHandleIntent(Intent intent) method. It is as easy as this example:

public class BackgroundConnectionService extends IntentService {

    public BackgroundConnectionService() {
        // Need this to name the service
        super ("ConnectionServices");
    }

    @Override
    protected void onHandleIntent(Intent arg0) {
        // Do stuff that you want to happen asynchronously here
        DefaultHttpClient httpclient = new DefaultHttpClient ();
        HttpGet httpget = new HttpGet ("http://www.google.com");
        // Some try and catch that I am leaving out
        httpclient.execute (httpget);
    }
}

最后,像在 AndroidManifest.xml 文件中的 <application> 标签内的任何普通服务一样声明异步服务.

Finally, declare the asynchronous service as you would any normal service in the AndroidManifest.xml file within the <application> tags.

...
        <service android:name="com.test.services.BackgroundConnectionService">
            <intent-filter>
                <action android:name="com.test.services.BackgroundConnectionService" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </service>
...

那应该差不多了.其实很简单:D

That should about do it. It is actually pretty easy : D

这篇关于Android - httpclient 作为后台服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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