android app通过AIDL绑定服务获取服务相关属性值

    本文主要讲解如何通过AIDL绑定服务获取服务接口的方法
    新建一个AS项目,布局里面添加三个button

    
    

    将服务的AIDL拷贝到项目同等路径下


image.png

    然后编写onclick事件

package com.qiyi.controllerbind;

import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.content.ServiceConnection;
import android.content.ComponentName;
import android.os.RemoteException;
import android.content.Intent;
import android.widget.Toast;

import com.android.qiyicontroller.AIDLController;
import com.android.qiyicontroller.AIDLListener;

public class MainActivity extends AppCompatActivity{
    private final String TAG = "ControllerInterface";
    private ServiceConnection conn = null;//链接成员变量,绑定服务的时候要去建立连接
    private static AIDLController myAIDLController = null;//获取服务实例

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

    public void clickbind(View v){
        Log.d(TAG, "bt1 bind() event");
        //bind here
        bind();//绑定服务
    }

    public void clickunbind(View v){
        Log.d(TAG, "bt2 unbind() event");
        //unbind here
        unbind();解绑服务
    }

    public void clickgetconn(View v){
        Log.d(TAG, "bt3 getParameter event");
        //get conn
        getParameters();获取属性
    }

    private ServiceConnection controllerConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {//连接建立成功会触发

            myAIDLController = AIDLController.Stub.asInterface(service);

            if (myAIDLController != null) {
                try {
                    Log.d(TAG, "ServiceConnection success");
                    Toast.makeText(MainActivity.this, "ServiceConnection success", Toast.LENGTH_LONG).show();
                } catch (Exception localException) {
                    Log.w(TAG, "Exception localException");
                }
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d(TAG, "onServiceDisconnected");
            Toast.makeText(MainActivity.this, "onServiceDisconnected", Toast.LENGTH_LONG).show();

            myAIDLController = null;
        }
    };

    private void bind(){
        Log.d(TAG, "bind");
        Intent intent = new Intent();
        intent.setAction("com.android.qiyicontroller.BIND");
        intent.setComponent(new ComponentName("com.google.vr.vrcore", "com.android.qiyicontroller.AIDLControllerService"));
        intent.setPackage("com.qiyi.controllerbind");
        this.bindService(intent,controllerConnection,this.BIND_AUTO_CREATE);//绑定服务,建立链接
    }

    private void unbind(){
        if(controllerConnection != null && myAIDLController.asBinder().isBinderAlive()){
            try{
                Log.d(TAG, "this.unbindService(controllerConnection);");
                this.unbindService(controllerConnection);
            } catch (Exception localException) {
                Log.w(TAG, "unbind Exception localException");
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.w(TAG, "onDestroy");
        if (controllerConnection != null) {
            unbind();
            controllerConnection = null;
        }
    }

    public String getParameters(){
        String connState = null;
        Log.w(TAG, "getParameters");
        if(myAIDLController != null){
            try {
                connState = myAIDLController.getParameters("controller_state");
                Log.w(TAG, "getParameters connState = " + connState);
                Toast.makeText(MainActivity.this, "getParameters connState = "+connState, Toast.LENGTH_LONG).show();
            }catch (RemoteException e) {
                e.printStackTrace();
                Log.w(TAG, "getParameters printStackTrace");
            }
        }
        return  connState;
    }
}

你可能感兴趣的:(android app通过AIDL绑定服务获取服务相关属性值)