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

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

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:orientation="vertical">
    <Button
        android:id = "@+id/btn_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="绑定服务"
        android:onClick="clickbind"
        android:layout_marginTop="10dp"
        android:background="#00ffbb"
        />

    <Button
        android:id = "@+id/btn_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="解锁绑定"
        android:onClick="clickunbind"
        android:layout_marginTop="10dp"
        android:padding="10dp"
        android:background="#00ffbb"
        />
    <Button
            android:id = "@+id/btn_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="获取连接状态"
            android:onClick="clickgetconn"
            android:layout_marginTop="10dp"
            android:padding="10dp"
            android:background="#00ffbb"
            />
    </LinearLayout>

    将服务的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 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,335评论 25 709
  • 面试必背 会舍弃、总结概括——根据我这些年面试和看面试题搜集过来的知识点汇总而来 建议根据我的写的面试应对思路中的...
    luoyangzk阅读 11,773评论 6 173
  • 自从去年十一月加入007,虽然写作水平不怎么样,但我对每次的作业,都当成是考试一样认真对待,每一篇文章都来自于生活...
    卿之生活随笔阅读 2,738评论 0 1
  • 女儿故事总结关键词用的比较好,事情经过一目了然
    猫儿嘴阅读 4,929评论 0 1
  • 文/鸿运 我的心灵是一块明镜 清清澈澈 如湖水见底 分外透明 我的心灵是一座小城 只容下诗词歌赋 字里行间都是童真...
    HONGYUNDANGTOU阅读 2,642评论 14 8