码迷,mamicode.com
首页 > 移动开发 > 详细

Android中使用IntentService执行后台任务

时间:2014-10-16 15:07:52      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:android   blog   http   io   os   使用   ar   java   for   

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


Android中使用IntentService执行后台任务

标签:android   blog   http   io   os   使用   ar   java   for   

原文地址:http://blog.csdn.net/goldenfish1919/article/details/40146611

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!