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