• AIDL安卓接口定义语言


    Android    Interface Definition  Language简称AIDL翻译为 :安卓       接口      定义        语言

    AIDL:进程间通信。Android Interface Defination Language。(使用接口回调的思想)

    举例:

    B应用程序中有一个服务,功能是做加法运算。
    A应用程序需要执行B应用中的加法功能。
    A中产生2个加数,将数据传给B应用的服务。B进行运算,将运算结果返回给A。
    我们要定义一个AIDL接口规范进程间的通信,AIDL接口的代码格式和传统的Java有些不同

    AIDL使用的前提:Service的绑定方式。
    onCreate 创建
    onBind 绑定
    onUnbind 解除绑定
    onDestroy 销毁

    故事:
    板砖A(Activity)和板砖S(Service)
    1、去五金商店买胶水
    2、将胶水涂在S表面上
    3、将A板砖粘在S表面上(注意:A和S中间有胶水)
    4、检测A和S是否粘牢
    5、如果粘牢,拿去拍人吧

    代码中
    1、创建胶水类Binder
    2、在Service的onBind方法中初始化胶水
    3、使用Activity去绑定Service
    4、通过一个接口来确定Activity和Service是否连接成功
    5、如果连接成功,A程序的Activity就可以使用B程序的Service了

    思路:
    先实现B程序的Service,具有加法运算功能
    再实现A程序的Activity,去绑定Service

    服务端:

    /**
     * 服务端,包含一个Service,运用执行加法运算
     * 1、创建进程间通信使用的AIDL接口
     * 2、实现Service
     * 3、等待客户端访问该Service
     */
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    }
    
    
    
    public class PlusService extends Service {
        public PlusService() {
        }
    
        // 创建
        @Override
        public void onCreate() {
            super.onCreate();
            Log.d("1507", "Service onCreate");
        }
    
        // 1、创建胶水类
        // stub:本意为存根。AIDL提供的类似于胶水的类,在这个胶水中实现了加法运算
        private static class MyBinder extends IMyAidlInterface.Stub {
    
            @Override
            public int plus(int plus01, int plus02) throws RemoteException {
                return plus01 + plus02;
            }
        }
    
        // 绑定
        @Override
        public IBinder onBind(Intent intent) {
            Log.d("1507", "Service onBind");
            return new MyBinder();
        }
    
        // 解绑
        @Override
        public boolean onUnbind(Intent intent) {
            Log.d("1507", "Service onUnbind");
            return super.onUnbind(intent);
        }
    
        // 销毁
        @Override
        public void onDestroy() {
            Log.d("1507", "Service onDestroy");
            super.onDestroy();
        }
    }

    清单文件:

     <service
                android:name=".service.PlusService"
                android:enabled="true"
                android:exported="true">
    
                <intent-filter>
                    <action android:name="bind_server"/>
                </intent-filter>
    
            </service>

    客户端:

    /**
     * 3、绑定服务端的Service
     * 4、通过ServiceConnection接口确定是否连接成功
     * 5、如果连接成功,调用Service的操作
     */
    public class MainActivity extends AppCompatActivity implements
            View.OnClickListener, ServiceConnection {
    
        // 自定义Action
        public static final String ACTION_BIND = "bind_server";
    
        protected EditText mPlus01Et;
        protected EditText mPlus02Et;
        protected Button mCalculateBtn;
        protected TextView mResultTv;
        private IMyAidlInterface mPlusInterface;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Log.d("1507", "Activity onCreate");
            super.setContentView(R.layout.activity_main);
            initView();
            bindService();
        }
    
        // 绑定Service
        private void bindService() {
            Intent intent = new Intent(ACTION_BIND);// action
            intent.setPackage("net.bwie.aidlserver");// server端对应的包名
            boolean isBindSuccessful = bindService(intent, this, Context.BIND_AUTO_CREATE);
            Toast.makeText(this, "" + isBindSuccessful, Toast.LENGTH_SHORT).show();
        }
    
        @Override
        public void onClick(View view) {
            if (view.getId() == R.id.calculate_btn) {
                int plus01 = Integer.parseInt(mPlus01Et.getText().toString());
                int plus02 = Integer.parseInt(mPlus02Et.getText().toString());
    
                // 调用AIDL接口中的方法
                try {
                    int result = mPlusInterface.plus(plus01, plus02);
                    mResultTv.setText("结果是:" + result);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }
  • 相关阅读:
    一步一步学lucene——(第四步:搜索篇)
    如何搭建sshpermissions工程
    如何选择ESB
    一步一步学Mule ESB——(第二篇:Ajax篇)
    一步一步学lucene——(第二步:示例篇)
    一步一步学Mule ESB——(第一篇:基础篇)
    三步学会用spring开发OSGI——(第三步:web篇)
    关于SourceForge不能使用的问题
    一步一步学lucene——(第三步:索引篇)
    常用px,pt,em换算表
  • 原文地址:https://www.cnblogs.com/SongYongQian/p/7822843.html
Copyright © 2020-2023  润新知