Android中IntentService详解

来源:互联网 发布:淘宝评论上穿不了照片 编辑:程序博客网 时间:2024/04/29 11:20
原文  http://www.cnblogs.com/weizilong/p/4040576.html
主题 Android

简单说,IntentService是继承于Service并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统Service一样,同时,当任务执行完后,IntentService会自动停止,而不需要我们去手动控制。另外,可以启动IntentService多次,而每一个耗时操作会以工作队列的方式在IntentService的onHandleIntent回调方法中执行,并且,每次只会执行一个工作线程,执行完第一个再执行第二个,以此类推。

而且,所有请求都在一个单线程中,不会阻塞应用程序的主线程(UI Thread),同一时间只处理一个请求。

那么,用IntentService有什么好处呢?首先,我们省去了在Service中手动开线程的麻烦,第二,当操作完成时,我们不用手动停止Service。

接下来让我们来看看如何使用,写一个Demo来模拟两个耗时操作,Operation1与Operation2,先执行1,2必须等1执行完才能执行:

新建工程,新建一个继承IntentService的类,我这里是IntentServiceDemo.java

public class IntentServiceDemo extends IntentService {  public IntentServiceDemo() {    //必须实现父类的构造方法    super("IntentServiceDemo");  }    @Override  public IBinder onBind(Intent intent) {    System.out.println("onBind");    return super.onBind(intent);  }  @Override  public void onCreate() {    System.out.println("onCreate");    super.onCreate();  }  @Override  public void onStart(Intent intent, int startId) {    System.out.println("onStart");    super.onStart(intent, startId);  }  @Override  public int onStartCommand(Intent intent, int flags, int startId) {    System.out.println("onStartCommand");    return super.onStartCommand(intent, flags, startId);  }  @Override  public void setIntentRedelivery(boolean enabled) {    super.setIntentRedelivery(enabled);    System.out.println("setIntentRedelivery");  }  @Override  protected void onHandleIntent(Intent intent) {    //Intent是从Activity发过来的,携带识别参数,根据参数不同执行不同的任务    String action = intent.getExtras().getString("param");    if (action.equals("oper1")) {      System.out.println("Operation1");    }else if (action.equals("oper2")) {      System.out.println("Operation2");    }        try {      Thread.sleep(2000);    } catch (InterruptedException e) {      e.printStackTrace();    }  }  @Override  public void onDestroy() {    System.out.println("onDestroy");    super.onDestroy();  }}

我把生命周期方法全打印出来了,待会我们来看看它执行的过程是怎样的。接下来是Activity,在Activity中来启动IntentService:

public class TestActivity extends Activity {  /** Called when the activity is first created. */  @Override  public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);        //可以启动多次,每启动一次,就会新建一个work thread,但IntentService的实例始终只有一个    //Operation 1    Intent startServiceIntent = new Intent("com.test.intentservice");    Bundle bundle = new Bundle();    bundle.putString("param", "oper1");    startServiceIntent.putExtras(bundle);    startService(startServiceIntent);        //Operation 2    Intent startServiceIntent2 = new Intent("com.test.intentservice");    Bundle bundle2 = new Bundle();    bundle2.putString("param", "oper2");    startServiceIntent2.putExtras(bundle2);    startService(startServiceIntent2);  }}

最后,别忘了配置Service,因为它继承于Service,所以,它还是一个Service,一定要配置,否则是不起作用的,开始我就是忘了,结果半天没反应。

<service android:name=".IntentServiceDemo">      <intent-filter >          <action android:name="com.test.intentservice"/>      </intent-filter></service>

ok,最后来看看执行结果:

从结果可以看到,onCreate方法只执行了一次,而onStartCommand和onStart方法执行了两次,开启了两个Work Thread,这就证实了之前所说的,启动多次,但IntentService的实例只有一个,这跟传统的Service是一样的。Operation1也是先于Operation2打印,并且我让两个操作间停顿了2s,最后是onDestroy销毁了IntentService。

这就是IntentService,一个方便我们处理业务流程的类,它是一个Service,但是比Service更智能。

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 纯银手链发黑怎么办 黄铜饰品变黑了怎么办 亲戚一直住我家怎么办 拿到调档函后怎么办 成都怎么办5年居住证 换单位了住房公积金怎么办 单位不交住房公积金怎么办 居转户没有离职证明怎么办 外地户口审驾照怎么办 居住证被注销了怎么办 新到上海怎么办居住证 战网积分过期怎么办 代理一年无赢利怎么办 开庭后不判决怎么办 离婚判决书没了怎么办 去英国工作签证怎么办 在美国怎么办英国签证 换护照英国签证怎么办 英国签证前咳嗽怎么办 英国留学被退学怎么办 在澳洲怎么办韩国签证 美签迟到了怎么办 签证照片贴错怎么办 过隧道耳朵难受怎么办 跑货车没货源怎么办 改文职老职工怎么办 铁路办家属证怎么办? 辐射4电梯故障怎么办 车辆被恶意损坏怎么办 汽车划伤见底怎么办 汽车被刀片划伤怎么办 汽车被笔画了怎么办 车被划了一条线怎么办 微信附近人上门被骗怎么办 交通事故认定书不服怎么办 自动挡下坡刹车失灵怎么办 自动挡汽车刹车失灵怎么办 重车刹车失灵怎么办 12306买票待核验怎么办 单位分流不想去怎么办 公司降薪不同意怎么办