Android中使用IntentService运行后台任务
Posted yutingliuyl
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
以上是关于Android中使用IntentService运行后台任务的主要内容,如果未能解决你的问题,请参考以下文章
在Android O中运行后台运行Intentservice
Android Training - 使用IntentService运行任务(Lesson 2 - 发送任务给IntentService)
Android:如何确定 IntentService 是不是正在运行?