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:
參考: