一、什么是Service?
service就是android系统中的服务,它有这么几个特点:它无法与用户直接进行交互、它必须由用户或者其他程序显式的启动、它的优先级比较高,它比处于前台的应用优先级低,但是比后台的其他应用优先级高,这就决定了当系统因为缺少内存而销毁某些没被利用的资源时,它被销毁的概率很小哦。
二、为什么要使用Service?
我们在做项目过程中,经常遇到一些任务需要再后台执行(比如后台播放音乐、上传、下载等),这个时候就要用到Service。
三、Service有几种启动方式?
两种
startService:
0、 新建类继承Service
public class MusicService extends Service
- 1
重写onCreate方法
Service在创建时调用的方法,可以用来做数据初始化,Service在没有停止之前,只会执行一次。实现onBind方法
由于Service是一个抽象类,定义了一个onBind抽象方法,所以必须实现此抽象方法,return null 即可,此方法在startService方式用不上,无需理会。重写onStartCommand方法
Service创建后会调用onStartCommand,此方法中可以接收调用者传递过来的参数,并且可以编写需要的逻辑代码,当重复调用Service时,onStartCommand会重复执行,onStart方法已弃用。重写onDestroy方法
Service在退出时调用,此方法中可以编写释放资源的操作。在AndroidManifest文件中注册Service
<service android:name="com.lenovo.mymediaplayer.service.MusicService" >
</service>
- 1
- 2
- 使用startService方法启动Service
Intent serviceIntent = new Intent(MusicActivity.this, MusicService.class);
// 绑定服务,启动
startService(serviceIntent);
- 1
- 2
- 3
- 使用stopService方法停止Service
bindService:
- step1:新建类继承Service
- step2:实现onBind方法
这里不同于startService中实现onBind方法那样简单。这里需要在此方法中返回一个Binder子类对象。 - step3:重写onCreate方法
- step4:重写onUnBind方法
在调用unbindService方法解除绑定后,触发此回调函数 - step5:重写onDestroy方法
- step6:在AndroidManifest中注册Service
- step7:实现ServiceConnection接口
这里是难点,通过代码示范。 - step8:通过bindService绑定Service
- step9:通过unbindService解绑Service
四、startService的特点及优缺点?
使用startService启动Service的总结
- onCreate只会执行一次,只要调用startService,onStartCommand一定会执行
- Service运行在main线程中,做耗时操作需另开子线程。
- 通过Intent进行传参,在onStartCommand中接收参数。
- 无法获得Service对象,不能直接操作Service中的属性和方法。
- 调用stopService后,Service会执行onDestroy方法后停止。
五、startService使用示例(附代码)
package com.example.administrator.servicedemo; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.support.annotation.Nullable; import android.util.Log; /** * Created by Administrator on 2018/3/21 0021. */ public class MyService extends Service { @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); Log.e("Service"+Thread.currentThread().getName(),"onCreate..."); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e("Service","onStartCommand..."); new Thread(new Runnable() { @Override public void run() { for (int i=0;i<10;i++){ Log.e("Service"+Thread.currentThread().getName(),i+"***"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); Log.e("Service"+Thread.currentThread().getName(),"onDestroy..."); } }
六、bindService的特点及优缺点?
使用bindService方式绑定Service的总结:
- onCreate,onBind只会执行一次
- Service运行在main线程中,做耗时操作需另开子线程。
- 可以直接操作Service中的属性和方法。
- 调用unbindService后,Service会执行onUnbind,onDestroy方法后停止。
七、bindService使用示例(附代码)
package com.example.administrator.servicedemo; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.support.annotation.Nullable; import android.util.Log; /** * Created by Administrator on 2018/3/23 0023. */ public class TestService extends Service { private String TAG="TestService"; private Guanjia guanjia=new Guanjia(); public class Guanjia extends Binder{ //作用:得到当前Service对象 public TestService getServiceObjecct(){ return TestService.this; } } public void fly(){ Log.e(TAG,"开飞机"); } @Nullable @Override public IBinder onBind(Intent intent) { Log.e(TAG,"onBind..."); return guanjia; } @Override public boolean onUnbind(Intent intent) { Log.e(TAG,"onUnbind..."); return super.onUnbind(intent); } @Override public void onCreate() { super.onCreate(); Log.e(TAG,"onCreate..."); } @Override public void onDestroy() { super.onDestroy(); Log.e(TAG,"onDestroy..."); } }
八、IntentService的特点及优缺点?
IntentService是基于消息队列(MessageQueue)的服务,,每次启动服务并不会马上处理工作,而是首先创建对应的Looper、Handler。任务的执行顺序即queue队列Message的顺序(排队执行)。任务一旦完成,将会自行停止。因此其使用范围多在以下几种情况:
更新客户端版本
需要在后台执行单任务
多个允许异步执行的后台任务(无前后顺序和时间顺序的要求)
优缺点:
使用方便,避开了我们自己需要Service中创建线程。
由于只有一个工作线程,所有任务的执行都需要排队,因为不适合对任务并发的情况。
九、IntentService使用示例(附代码)
package com.example.administrator.servicedemo; import android.app.IntentService; import android.content.Intent; import android.support.annotation.Nullable; import android.util.Log; /** * Created by Administrator on 2018/3/23 0023. */ class MyIntentService extends IntentService { public MyIntentService(String name) { super(name); } @Override protected void onHandleIntent(@Nullable Intent intent) { for (int i = 0; i < 10; i++) { try { Log.e("MyIntentService", i + "***"); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } @Override public void onCreate() { super.onCreate(); } @Override public void onDestroy() { super.onDestroy(); } }
十、IntentService和Service的区别是?
1. 首先IntentService是继承自Service;2. Service不是一个单独的进程,它和应用程序在同一个进程中;
3. Service也不是一个线程,所以我们要避免在Service中进行耗时的操作;
4. IntentService使用队列的方式将请求的Intent加入队列,然后开启了一个Worker Thread(工作线程)在处理队列中的Intent,对于异步的startService请求,
IntentService会处理完成一个之后在处理第二个,每一个请求都会在一个单独的Worker Thread中处理,不会阻塞应用程序的主线程。
因此,如果我们如果要在Service里面处理一个耗时的操作,我们可以用IntentService来代替使用。
5. 使用IntentService 必须首先继承IntentService并实现onHandleIntent()方法,将耗时的任务放在这个方法执行,其他方面,IntentService和Service一样。