Background processing with IntentService class

本文介绍如何利用Android SDK中的IntentService类来卸载应用程序主线程的任务,进行后台处理。通过示例代码展示了如何启动IntentService,处理后台任务并接收处理结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

This article explain the usage of IntentService class in android SDK to perform background processing to offload tasks from an application’s main thread (Your activity).

when IntentService jobs done, the results are sent back to the activity.

Good example whould be an application that uses background processing for REST calls; to download some data from the internet, parse it, and send it back to the application.
There are several ways to do background (asynchronous) processing tasks:

while the 3rd options is too compilcated for this purpose you should consider one of the others generaly.
ASyncTask enables proper and easy use of the UI thread.
Service is an application component representing either an application’s desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use.
IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand.
With AsyncTask if the user goes to another Activity you can’t transfer that object to the other Activity so it dies.
You can communicate between Service and Activity, but it’s tricky to do it right using Binders.

IntentService

Class Overview:

  • Since: API Level 3
  • IntentService is a base class for 
    1
    <a href="http://developer.android.com/reference/android/app/Service.html">Service</a>

    s that handle asynchronous requests

  • Clients send requests through 
    1
    <a href="http://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent)">startService(Intent)</a>

     calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.

  • All requests are handled on a single worker thread — they may take as long as necessary (and will not block the application’s main loop), but only one request will be processed at a time.

You should read more on the Class overview at developer.android.com.

Class Usage:

To use it, extend IntentService and implement 

1
<a href="http://developer.android.com/reference/android/app/IntentService.html#onHandleIntent(android.content.Intent)">onHandleIntent(Intent)</a>

. IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.

Base IntentService

Before I will continue with a full example of using IntentService you can look at how should a base IntentService looks:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import android.app.IntentService;
import android.content.Intent;
 
public class Downloader extends IntentService {
 
   public Downloader(String name) {
      super(name);
      // TODO Auto-generated constructor stub
   }
 
   @Override
   protected void onHandleIntent(Intent arg0) {
      // TODO Auto-generated method stub
 
   }
}
  • IntentService has a single constructor that takes a string argument
    name“. It’s only use is in naming the
    worker thread for the IntentService.
  • onHandleIntent(): This method is invoked on the worker thread with a request to process.
  • You should not override onStartCommand method for your IntentService.

IntentService Example

BgProcessingActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package itekblog.examples;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;
 
public class BgProcessingActivity extends Activity implements BgProcessingResultReceiver.Receiver {
 
    public BgProcessingResultReceiver mReceiver;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Toast.makeText(this, "Starting background processing...", Toast.LENGTH_LONG).show();
        // register a reciever for IntentService broadcasts
        mReceiver = new BgProcessingResultReceiver(new Handler());
        mReceiver.setReceiver(this);
         // start IntentService
         final Intent intent = new Intent(Intent.ACTION_SYNC, null, this, BgProcessingIntentService.class);
        // optional: send Extra to IntentService
        // intent.putExtra("name", "value");
         intent.putExtra("receiver", mReceiver);
         intent.putExtra("command", "query");
         startService(intent);
    }   
 
    @Override
    public void onReceiveResult(int resultCode, Bundle resultData) {
        switch (resultCode) {
        case BgProcessingIntentService.STATUS_RUNNING:
            //show progress
            Toast.makeText(this, "Running", Toast.LENGTH_LONG).show();
            break;
        case BgProcessingIntentService.STATUS_FINISHED:
            // hide progress & analyze bundle
            Toast.makeText(this, "OK", Toast.LENGTH_LONG).show();
            break;
        case BgProcessingIntentService.STATUS_ERROR:
            // handle the error;
            Toast.makeText(this, "Error", Toast.LENGTH_LONG).show();
            break;
        }
    }   
 
    @Override
    public void onPause() {
        super.onPause();
        if (mReceiver!=null) mReceiver.setReceiver(null); // clear receiver to avoid leaks.
    }
 
}

BgProcessingIntentService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package itekblog.examples;
 
import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.os.ResultReceiver;
import android.util.Log;
 
public class BgProcessingIntentService extends IntentService {
 
    public static final int STATUS_RUNNING = 0;
    public static final int STATUS_FINISHED = 1;
    public static final int STATUS_ERROR = 2;
 
    public BgProcessingIntentService() {
        super(BgProcessingIntentService.class.getName());
    }
 
    @Override
    protected void onHandleIntent(Intent arg0) {
        Log.d(BgProcessingIntentService.class.getName(), "service started...");
        // sendBroadcast();
 
        final ResultReceiver receiver = arg0.getParcelableExtra("receiver");
        String command = arg0.getStringExtra("command");
        Bundle b = new Bundle();
        if(command.equals("query")) {
            receiver.send(STATUS_RUNNING, Bundle.EMPTY);
            try {
                // get some data or something
                //b.putParcelableArrayList("results", results);
                receiver.send(STATUS_FINISHED, b);
            } catch(Exception e) {
                b.putString(Intent.EXTRA_TEXT, e.toString());
                receiver.send(STATUS_ERROR, b);
            }
        }
        Log.d(BgProcessingIntentService.class.getName(), "service stopping...");
        this.stopSelf();
    }
 
}

BgProcessingResultReceiver.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package itekblog.examples;
 
import android.os.Bundle;
import android.os.Handler;
import android.os.ResultReceiver;
 
public class BgProcessingResultReceiver extends ResultReceiver {
    private Receiver mReceiver;
 
    public BgProcessingResultReceiver(Handler handler) {
        super(handler);
    }
 
    public void setReceiver(Receiver receiver) {
        mReceiver = receiver;
    }
 
    public interface Receiver {
        public void onReceiveResult(int resultCode, Bundle resultData);
    }
 
    @Override
    protected void onReceiveResult(int resultCode, Bundle resultData) {
        if (mReceiver != null) {
            mReceiver.onReceiveResult(resultCode, resultData);
        }
    }
}

AndroidManifest.xml

1
<!--?xml version="1.0" encoding="utf-8"?-->

don’t forget to register both files in your AndroidManifest.xml:

this method should work perfectly and the REST call is running in a background process, updating the activity when done.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值