android学习笔记(13) intent Services多线程初步

﹏ヽ暗。殇╰゛Y 2022-08-04 14:54 283阅读 0赞

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:

  1. package com.example1.servicedemo;
  2. import org.apache.commons.logging.Log;
  3. import android.net.sip.SipAudioCall.Listener;
  4. import android.os.Bundle;
  5. import android.app.Activity;
  6. import android.content.Intent;
  7. import android.view.Menu;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.widget.Button;
  11. public class MainActivity extends Activity {
  12. protected static final String TAG = "MainActivity";
  13. private Button btnButton;
  14. private Button btnButton2;
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_main);
  19. btnButton = (Button)findViewById(R.id.btnstartnormalservice);
  20. btnButton2 = (Button)findViewById(R.id.btnstartintentservice);
  21. btnButton.setOnClickListener(listener);
  22. btnButton2.setOnClickListener(listener);
  23. }
  24. private OnClickListener listener = new OnClickListener() {
  25. @Override
  26. public void onClick(View v) {
  27. switch (v.getId()) {
  28. case R.id.btnstartnormalservice:
  29. Intent intent = new Intent(MainActivity.this,ExampleService.class);
  30. Log.i(TAG,"主线程ID:"+Thread.currentThread().getId());//输出当前线程ID
  31. startService(intent);
  32. break;
  33. case R.id.btnstartintentservice:
  34. Intent intent2 = new Intent(MainActivity.this,ExampleIntentService.class);
  35. Log.i(TAG,"主线程ID:"+Thread.currentThread().getId());//输出当前线程ID
  36. startService(intent);
  37. break;
  38. default:
  39. break;
  40. }
  41. }
  42. };
  43. }

ExampleIntentService.java

  1. package com.example1.servicedemo;
  2. import android.app.IntentService;
  3. import android.content.Intent;
  4. public class ExampleIntentService extends IntentService {
  5. private static final String tag = "ExampleIntentService";
  6. public ExampleIntentService(String name) {
  7. super("ExampleIntentService");
  8. }
  9. @Override
  10. protected void onHandleIntent(Intent arg0) {
  11. try {
  12. android.util.Log.i(tag,"Exampleservice线程ID:"+Thread.currentThread().getId());
  13. android.util.Log.i(tag,"文件下载...");
  14. Thread.sleep(2000);
  15. } catch (InterruptedException e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. }

记得在XML中声明服务!!:

结果:

多次点击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

文件下载…

发表评论

表情:
评论列表 (有 0 条评论,283人围观)

还没有评论,来说两句吧...

相关阅读

    相关 线学习笔记

    1.进程 线程 进程(Process) 是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础。进程是执行程序的一次执行

    相关 Android学习笔记----Intent

    下面讲解一下Android中的Intent的内容 Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作、动作涉及数据、附加数