Service

一、什么是Service

Service是Android的四大组件之一,和Activity级别相当
Service是可以长时间运行在后台的,是不可见、是没有界面的组件
Service是运行在主线程的
Service可以跨进程调用

二、为什么要使用Service?

当某个功能需要执行很长时间(如:下载、收集数据等),并且在这个过程中,不需要让用户操作,不需要Activity进行交互的情况下,可以通过Service在后台完成指定任务。

三、Service的启动方式

Service基本上分为两种形式:startService、bindService.

四、startService的特点及其优点与缺点

onCreate只会执行一次,只要调用startService,onStartCommand一定会执行
Service运行在main线程中,做耗时操作需要例外开子线程
通过Intent进行传参,在onStartCommand接收参数
无法获得Service对象,直接操作Service中的属性和方法
调用stopService后,Service会执行onDestory后停止

优点:

使用简单,和Activity一样,只需要几行代码即可启动Service

缺点:

使用startService的方式,无法获得Service对象,不能直接操作Service中的属性和方法,只能通过Intent进行传递不同参数,重复调用startService,触发onStartCommand。

五、使用startService方式启动Service的步骤

  1. 新建类继承Service
  2. 重写onCreate方法(在创建时调用的方法,可以用来做数据初始化,Service在没有停止之前,只会执行一次)
  3. 实现onBind抽象方法
  4. 重写onStartCommand(创建后会调用onStartCommand,此方法中可以接收调用者传递过来的参数,并且可以编写需要的逻辑代码,当重复调用Service时,onStartCommand会重复执行)
  5. 重写onDestroy方法 (在退出时调用,此方法中可以编写释放资源的操作)
  6. 在AndroidManifest文件中注册Service(使用标签注册)
  7. 使用startService方法启动Service
  8. 使用stopService方法停止Service

六、使用startService方式的代码示范

首先需要在布局文件中设置两个按钮,用来启动和停止Service

然后在Activity文件中对按钮进行定义、绑定ID、设置监听和点击事件

最后在新建的类中写入startService方式启动Service的步骤

 <Button
        android:id="@+id/start_service"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="start_service"/>
    <Button
        android:id="@+id/stop_service"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="stop_service"/>
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Button startBtn;
    private Button stopBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bindID();
    }

    private void bindID() {
        startBtn=findViewById(R.id.start_service);
        stopBtn=findViewById(R.id.stop_service);

        startBtn.setOnClickListener(this);
        stopBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.start_service:
                Intent startIntent=new Intent(this,MyService.class);
                startService(startIntent);//启动service

                break;
            case R.id.stop_service:
                Intent stopIntent=new Intent(this,MyService.class);
                stopService(stopIntent);//停止service

                break;
                default:
                    break;
        }
    }
}
public class MyService extends Service {


    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("MyService"+Thread.currentThread().getName(),"onCreate executed");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("MyService","onStartCommand executed");


        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=0;i<10;i++){
                    Log.e("MyService"+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("MyService"+Thread.currentThread().getName(),"onDestroy executed");
    }

}

七、bindService的特点及其优缺点

onCreate,onBind只会执行一次。
Service运行在main线程中,做耗时操作需另开子线程。
可以直接操作Service中的属性和方法。
调用unbindService后,Service会执行onUnbind,onDestroy方法后停止。

优点:

可以得到Service对象,灵活控制Service内部的属性和方法。

缺点:

使用较为复杂

八、使用bindService方式启动Service的步骤

01、新建类继承Service
02、实现onBind方法 (这里不同于startService中实现onBind方法那样简单。这里需要在此方法中返回一个Binder子类对象。)
03、重写onCreate方法
04、重写onUnBind方法 (在调用unbindService方法解除绑定后,触发此回调函数)
05、:重写onDestroy方法
06、在AndroidManifest中注册Service
07、实现ServiceConnection接口 (这里是难点)
08、通过bindService绑定Service
09、通过unbindService解绑Service

九、使用bindService方式的代码示范

public class MyService2 extends Service {

private String TAG="MyService2";
private Guanjia guanjia=new Guanjia();

public class Guanjia extends Binder{
    //作用:得到Service对象
    public MyService2 getServiceObject(){
        return  MyService2.this;
    }
}
//定义一个方法
public  void fly(){
    Log.e(TAG,"开飞车......");
}
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        Log.e(TAG,"onBind......");
        return guanjia;

    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e(TAG,"onUnbind......");
        return super.onUnbind(intent);
    }

    @Override
    public void onCreate() {
        Log.e(TAG,"onCreate......");
        super.onCreate();
    }

    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);
    }

    @Override
    public void onDestroy() {
        Log.e(TAG,"onDestroy......");
        super.onDestroy();
    }
}
//Activity中的代码
public class Main2Activity extends AppCompatActivity implements View.OnClickListener{
//定义布局中的控件
    private Button button1;
    private Button button2;
    private Button button3;

    private ServiceConnection serviceConnection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            //通过管家的中介作用。拿到MyService2对象
            MyService2 myService2=((MyService2.Guanjia) iBinder).getServiceObject();
            myService2.fly();//调用定义的方法
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        bindID();
    }
//绑定ID
    private void bindID() {
        button1=findViewById(R.id.main2_btn1);
        button2=findViewById(R.id.main2_btn2);
        button3=findViewById(R.id.main2_btn3);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
    }
//设置点击事件
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.main2_btn1:
                Intent intent=new Intent(this,MyService2.class);
                bindService(intent,serviceConnection,BIND_AUTO_CREATE);//绑定
                break;
            case R.id.main2_btn2:
                unbindService(serviceConnection);//解绑
                break;
            case R.id.main2_btn3:
                Intent intent1=new Intent(Main2Activity.this,Main3Activity.class);
                Main2Activity.this.startActivity(intent1);//页面跳转
                break;
        }
    }
}

十、什么是IntentService

IntentService是继承于Service并处理异步任务的一个类
在IntentService内有一个工作线程来处理异步耗时操作
此线程无需我们控制和维护
当多次启动时,会以队列的形式,逐一执行
当执行完耗时操作后,此Service会自动停止

十一、使用IntentService的步骤

新建类继承IntentService
实现父类的构造方法
重写onHandleIntent()方法
重写onCreate方法
重写onDesttory方法
在AndroidManifest文件中注册Service(容易忘记或出错)
在有Content的环境下启动Service

十二、实例代码展示:

public class MyIntentService extends IntentService{
    public MyIntentService(String name) {
        super(name);
    }
    public MyIntentService(){
        super("");

    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        for (int i=0;i<5;i++){
            Log.e("MyIntentServive",i+"***");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
public class Main3Activity extends AppCompatActivity implements View.OnClickListener{
    private Button button1;
    private Button button2;
    private Button button3;

    String TAG="Main3Activity";
    public ServiceConnection serviceConnection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {

        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        bindID();
    }

    private void bindID() {
        button1=findViewById(R.id.main3_btn1);
        button2=findViewById(R.id.main3_btn2);
        button3=findViewById(R.id.main3_btn3);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.main3_btn1:
                Intent intent=new Intent(this,MyService2.class);
                bindService(intent,serviceConnection,BIND_AUTO_CREATE);
                break;
            case R.id.main3_btn2:
                unbindService(serviceConnection);
                break;
            case R.id.main3_btn3:
                Intent intent1=new Intent(this,MyIntentService.class);
                startService(intent1);
                break;
        }
    }
}

十三、Service与IntentService的区别

  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一样。
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值