首页 > 代码库 > Android中使用IntentService运行后台任务

Android中使用IntentService运行后台任务

IntentService提供了一种在后台线程中运行任务的方式,适合处理运行时间较长的后台任务。

长处:

(1)IntentService执行在单独的线程中,不会堵塞UI线程

(2)IntentService不受生命周期的影响

缺点:

(1)不能与UI直接进行交互,能够用Broadcast

(2)顺序运行请求,第二个请求仅仅有在第一个请求运行完以后才干运行

(3)请求不能被中断


使用IntentService的步骤:

(1)在Activity中通过startService启动service,并传递參数。

(2)Service中接收參数,做耗时的处理,处理完成,发送Broadcat。并把处理结果传递出来

(3)Activity中注冊BroadcastReceiver,监听广播。更新UI。


看一个样例:

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button btn = (Button) this.findViewById(R.id.btn);
		btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {//通过startService启动service,并传递參数。
				Intent mServiceIntent = new Intent(MainActivity.this,RSSPullService.class);
				mServiceIntent.setData(Uri.parse("http://www.baidu.com/"));
				MainActivity.this.startService(mServiceIntent);
			}
		});
		//注冊BroadcastReceiver。监听广播
		IntentFilter statusIntentFilter = new IntentFilter(Constants.BROADCAST_ACTION);
        // Sets the filter's category to DEFAULT
        statusIntentFilter.addCategory(Intent.CATEGORY_DEFAULT);
		DownloadStateReceiver mDownloadStateReceiver = new DownloadStateReceiver();
		// Registers the DownloadStateReceiver and its intent filters
		LocalBroadcastManager.getInstance(this).registerReceiver(mDownloadStateReceiver, statusIntentFilter);

	}

	private class DownloadStateReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			String data = http://www.mamicode.com/intent.getStringExtra(Constants.EXTENDED_DATA);>
public class RSSPullService extends IntentService {

	public RSSPullService() {
		super("RSSPullService");
	}
	
	@Override
	protected void onHandleIntent(Intent workIntent) {//接收參数。做耗时的处理,处理完成。发送Broadcat
		String localUrlString = workIntent.getDataString();
		String data = http://www.mamicode.com/download(localUrlString);>
public class Constants {

	// Defines a custom Intent action
	public static final String BROADCAST_ACTION = "com.example.android.threadsample.BROADCAST";

	// Defines the key for the status "extra" in an Intent
	public static final String EXTENDED_DATA_STATUS = "com.example.android.threadsample.STATUS";
	
	public static final String EXTENDED_DATA = http://www.mamicode.com/"com.example.android.threadsample.DATA";>
AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.intentservicedemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />

    <!-- Requires this permission to download RSS data from Picasa -->
    <uses-permission android:name="android.permission.INTERNET" />

    <!--
        Defines the application.
    -->
    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name">

        <activity
            android:name="com.example.android.intentservicedemo.MainActivity"
            android:label="@string/activity_title" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!--
            No intent filters are specified, so android:exported defaults to "false". The
            service is only available to this app.
        -->
        <service
            android:name="com.example.android.intentservicedemo.RSSPullService"
            android:exported="false"/>

    </application>

</manifest>

參考:http://developer.android.com/training/run-background-service/index.html


Android中使用IntentService运行后台任务