android学习笔记(13) intent Services多线程初步
2015-09-03 19:12
435 查看
对应若水老师的第十七课
IntentService是Service类的子类,用来处理异步请求,客户端通过startService(intent)方法传递请求给IntentService,IntentService通过worher thread处理每个Intent对象,执行完所有工作后自动停止Service.
IntentService执行如下操作:
*创建一个与应用程序线程分开的worker thread 用来处理所有通过学习onstartcommand()传递过来的Intent请求
*创建一个work queue,一次只传递一个intent到onHandleIntent()方法中,从而不用担心多线程带来的问题
*当处理完所有请求后自动停止服务,而不需要我们自己调用stopSelf()方法.
*默认实现了onBind()方法,返回值为null
*默认实现了onStartCommand()方法,这个方法将会把我们的intent放到work queue中,然后在onHandleIntent()中执行.
:
写构造方法
复写onHandleIntent()方法
源码对一般的多线程服务和intent service多线程对比:
MainActivity.java:
结果:
多次点击startnormalservice按键:
主线程ID:1
Exampleservice线程ID:9
文件下载...
主线程ID:1
Exampleservice线程ID:10
文件下载...
主线程ID:1
Exampleservice线程ID:11
文件下载...
多次点击startintentservice按键:
主线程ID:1
主线程ID:1
主线程ID:1
Exampleservice线程ID:9
文件下载...
Exampleservice线程ID:9
文件下载...
Exampleservice线程ID:9
文件下载...
IntentService是Service类的子类,用来处理异步请求,客户端通过startService(intent)方法传递请求给IntentService,IntentService通过worher thread处理每个Intent对象,执行完所有工作后自动停止Service.
IntentService执行如下操作:
*创建一个与应用程序线程分开的worker thread 用来处理所有通过学习onstartcommand()传递过来的Intent请求
*创建一个work queue,一次只传递一个intent到onHandleIntent()方法中,从而不用担心多线程带来的问题
*当处理完所有请求后自动停止服务,而不需要我们自己调用stopSelf()方法.
*默认实现了onBind()方法,返回值为null
*默认实现了onStartCommand()方法,这个方法将会把我们的intent放到work queue中,然后在onHandleIntent()中执行.
:
写构造方法
复写onHandleIntent()方法
源码对一般的多线程服务和intent service多线程对比:
MainActivity.java:
package com.example1.servicedemo; import org.apache.commons.logging.Log; import android.net.sip.SipAudioCall.Listener; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { protected static final String TAG = "MainActivity"; private Button btnButton; private Button btnButton2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnButton = (Button)findViewById(R.id.btnstartnormalservice); btnButton2 = (Button)findViewById(R.id.btnstartintentservice); btnButton.setOnClickListener(listener); btnButton2.setOnClickListener(listener); } private OnClickListener listener = new OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnstartnormalservice: Intent intent = new Intent(MainActivity.this,ExampleService.class); Log.i(TAG,"主线程ID:"+Thread.currentThread().getId());//输出当前线程ID startService(intent); break; case R.id.btnstartintentservice: Intent intent2 = new Intent(MainActivity.this,ExampleIntentService.class); Log.i(TAG,"主线程ID:"+Thread.currentThread().getId());//输出当前线程ID startService(intent); break; default: break; } } }; }ExampleIntentService.java
package com.example1.servicedemo; import android.app.IntentService; import android.content.Intent; public class ExampleIntentService extends IntentService { private static final String tag = "ExampleIntentService"; public ExampleIntentService(String name) { super("ExampleIntentService"); } @Override protected void onHandleIntent(Intent arg0) { try { android.util.Log.i(tag,"Exampleservice线程ID:"+Thread.currentThread().getId()); android.util.Log.i(tag,"文件下载..."); Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } }记得在XML中声明服务!!: <service android:name=".ExampleIntentService"/>
结果:
多次点击startnormalservice按键:
主线程ID:1
Exampleservice线程ID:9
文件下载...
主线程ID:1
Exampleservice线程ID:10
文件下载...
主线程ID:1
Exampleservice线程ID:11
文件下载...
多次点击startintentservice按键:
主线程ID:1
主线程ID:1
主线程ID:1
Exampleservice线程ID:9
文件下载...
Exampleservice线程ID:9
文件下载...
Exampleservice线程ID:9
文件下载...
相关文章推荐
- Android数据库--创建表和LitePal的基本用法
- 新浪微博开放平台提交审核时Android签名生成
- Android 使用DrawerLayout实现抽屉效果的导航菜单
- Android Material Design 动画实现
- android学习笔记(12) normal Services多线程初步
- 框架模式MVP在Android中的使用
- android切割音视频
- Android中jni工作流
- 深入讲解Android中Activity launchMode
- Android Version
- Android与设计模式:用单一职责则为Activity解耦
- Android开发之消息推送
- CSS绘制Android Robot
- Android and Me
- android 屏幕适配
- Android 基础学习——Activity四种启动模式
- Android开发之自定义控件与属性动画Animation的结合使用
- Android笔记:如何写出一个侧滑菜单
- android4.0 MediaPlayer的notify监听机制的全面剖析
- android中ListView的定位:使用setSelectionFromTop