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 = intent.getStringExtra(Constants.EXTENDED_DATA);
			Log.e("test", data);
			Toast.makeText(context, data, Toast.LENGTH_SHORT).show();
		}
	}

}

public class RSSPullService extends IntentService {

	public RSSPullService() {
		super("RSSPullService");
	}
	
	@Override
	protected void onHandleIntent(Intent workIntent) {//接收参数,做耗时的处理,处理完毕,发送Broadcat
		String localUrlString = workIntent.getDataString();
		String data = download(localUrlString);
		Intent localIntent = new Intent(Constants.BROADCAST_ACTION);
	    // Puts the status into the Intent
		localIntent.putExtra(Constants.EXTENDED_DATA, data);
	    // Broadcasts the Intent to receivers in this app.
	    LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
	}
	
	private String download(String localUrlString){
		try{
			URL url = new URL(localUrlString);
			HttpURLConnection conn = (HttpURLConnection)url.openConnection();
			InputStream in = conn.getInputStream();
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			byte[] buff = new byte[1024];
			int len = 0;
			while((len = in.read(buff)) != -1){
				out.write(buff,0,len);
			}
			in.close();
			return new String(out.toByteArray());
		}catch(Exception e){
			e.printStackTrace();
			return "";
		}
	}
}

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 = "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


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。