Service:bind方式开启服务

开启服务有两种方式:

1.startService
2.bindService
    a.第一次调用bindService开启服务,生命周期中会调用onCreate 和onBind方法   
    b.第二次接下再次bindService生命周期中方法不会被调用.
    c.bindService开启服务,这个Activity和服务不求同生,只求同死。activity退出时会执行service的onDestroy方法;  记得要解绑:unbindService
    d.bindServcie开启服务解绑只能做一次
    e.bindService 开启的服务在设置中是找不到的,这种方式开启的服务是一个隐形的服务,依赖于开启者

为什么要引入bind :绑定服务抽取接口

使用bindservice调用服务中方法的流程:

1.在服务中创建一个中间人的类, 继承Binder,实现一个接口,接口中定义服务向外暴露的方法,中间人类去实现这些方法,直接可以调用服务内部的方法。

     一.☆☆☆☆☆☆☆☆☆service中创建一个中间人的类,

        class MyBinder extends Binder implements  Iservice{

            //调用服务内办证方法
            public void callBanZheng() {
                banzhengMethod();
            }

            //调用服务内打麻将方法
            public void callDaMaJiang() {
                打麻将();
            }
            //调用服务内洗桑拿方法
            public void callXisangna() {
                    XiSangNa();
            }


        }
      二.☆☆☆☆☆☆☆☆☆定义一个接口,抽取中间人中的方法
        public interface Iservice {
            //中间人办证
            public void callBanZheng();

            //中间人打麻将
            public void callDaMaJiang();
            //中间人洗桑拿
            public void callXisangna();
        }

2.在服务中onBinder方法中创建一个中间人对象返回

     三.☆☆☆☆☆☆☆☆☆service中的onBinder方法中返回一个中间人对象
        @Override
        public IBinder onBind(Intent intent) {
            System.out.println("onBind");
            //创建中间人对象返回
            return new MyBinder();
        }

3.Activity中使用bindservice方式开启服务,需要创建一个ServiceConnect类,需要实现两个方法,onServiceConnected方法会将服务中的中间人对象传递回来,我们可以直接拿到中间人对象使用.

 四.☆☆☆☆☆☆☆☆☆activity中bind方式开启服务
    MyServiceConnect myServiceConnect ;
    //bind方式开启服务
    public void bindstartService(View v) {
        //创建一个intent对象
        Intent service = new Intent(this,BindTypeService.class);
        //bind方式开启绑定服务, service : intent;conn: 服务链接对象    flags:标记 ,默认 BIND_AUTO_CREATE
         myServiceConnect = new MyServiceConnect();
        bindService(service, myServiceConnect,  BIND_AUTO_CREATE);
    }
    五.☆☆☆☆☆☆☆☆☆在activity中MyServiceConnect中的onServiceConnected中获取中间人对象

    public Iservice myBinder ;
    //创建一个服务链接的子类
    public class MyServiceConnect implements  ServiceConnection{
        //服务链接成功时调用
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            System.out.println("onServiceConnected");
            //获取中间人对象
            myBinder =  (Iservice) service;
        }

        //服务失去链接时调用
        @Override
        public void onServiceDisconnected(ComponentName name) {
            System.out.println("onServiceDisconnected");
        }
    }
 六.☆☆☆☆☆☆☆☆☆activity中使用中间人对象调用服务中的方法

        //点击该按钮执行service中的一个方法
        public void banzheng(View v){
            //通过中间人对象调用办证方法
            myBinder.callBanZheng();
            myBinder.callDaMaJiang();
            myBinder.callXisangna();
        }

代码示例:

(1)新建Iservice.java,定义接口:

public interface Iservice {

    //中间人办证
    public void callBanZheng();

    //中间人打麻将
    public void callDaMaJiang();

    //中间人洗桑拿
    public void callXiSangNa();
}

(2)新建BindTypeService.java文件,编写代码如下:

//定义一个Service
public class BindTypeService extends Service {

    public BindTypeService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        System.out.println("onBind()执行");
        return new MyBinder();
    }




    //创建一个中间人的类,其中Binder是IBinder的子类
    class MyBinder extends Binder implements Iservice{

        //调用服务内办证的方法
        public void callBanZheng(){
            banzhengMethod();
        }

        //调用服务内打麻将的方法
        public void callDaMaJiang(){
            打麻将();
        }

        //调用服务内洗桑拿的方法
        public void callXiSangNa(){
            XiSangNa();
        }
    }


    public void XiSangNa(){
        Toast.makeText(this, "陪领导洗桑拿", Toast.LENGTH_SHORT).show();
    }

    public void 打麻将(){
        Toast.makeText(this, "陪领导打麻将", Toast.LENGTH_SHORT).show();
    }

    //自定义一个download方法
    public void banzhengMethod(){
        Toast.makeText(this, "办证完成", Toast.LENGTH_SHORT).show();
    }





    @Override
    public void onCreate() {
        System.out.println("onCreate()执行了");
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        System.out.println("onStartCommand()执行了");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        System.out.println("onDestroy()执行了");
        super.onDestroy();
}

(3)在MainActivity.java文件中编写如下代码:

public class MainActivity extends AppCompatActivity {

    //服务链接对象
    public MyServiceConnect myServiceConnect;
    //activity与service的中间人对象
    public Iservice myBinder;


    //点击该按钮执行service中的一个方法
    public void banzheng(View v){
        //通过中间人对象调用办证方法
        myBinder.callBanZheng();
        myBinder.callDaMaJiang();
        myBinder.callXiSangNa();
    }


    //bind方式开启服务
   public void bindstartService(View v){
        //创建一个intent对象
       Intent service = new Intent(this, BindTypeService.class);

        /////////bind方式开启服务,
        // service:intent;
        // conn:服务链接对象;
        // flag:绑定操作选项;
       myServiceConnect = new MyServiceConnect();
       bindService(service, myServiceConnect, BIND_AUTO_CREATE);
   }



    //创建一个服务链接的子类
    public class MyServiceConnect implements ServiceConnection{

        //服务链接成功时调用
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            System.out.println("onServiceConnected()");
            //获取中间人对象
            myBinder = (Iservice)service;
        }

        //服务失去链接时调用
        @Override
        public void onServiceDisconnected(ComponentName name) {
            System.out.println("onServiceDisconnected()");
        }
    }



    public void unbindService(View v) {
        unbindService(myServiceConnect);
    }

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

    @Override
    protected void onDestroy() {
        //当activity退出时,需要解绑服务
        unbindService(myServiceConnect);
        super.onDestroy();
    }
}

(4)布局文件中activity_main.xml内容如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="bindstartService"
        android:text="bind方式开启服务"
        android:textAllCaps="false"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="unbindService"
        android:text="解绑服务"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="banzheng"
        android:text="办证"/>

</LinearLayout>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章