android 实现AIDL之简单计算器

低调的学习着android,学习了基础之后,以下列表是下一步的计划。不像之前那样,现在不做记录已经不行了。一步一步来,今天写一下AIDL

 IDL:熟悉AIDL,理解其工作原理,懂transact和onTransact的区别;

- Binder:从Java层大概理解Binder的工作原理,懂Parcel对象的使用;

- 多进程:熟练掌握多进程的运行机制,懂Messenger、Socket等;

- 事件分发:弹性滑动、滑动冲突等;

- 玩转View:View的绘制原理、各种自定义View;

- 动画系列:熟悉View动画和属性动画的不同点,懂属性动画的工作原理;

- 懂性能优化、熟悉mat等工具

- 懂点常见的设计模式

AIDL 实现了不同进程之间的数据通信。首先看目录结构:

                                                             


实现步骤:

1 编写服务端aidl 文件:

 
// CalculateInterface.aidl
package com.yiran.push.aidlserver;

// Declare any non-default types here with import statements

interface CalculateInterface {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
      void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);

      double doCalculate(int a , int b);
}

二 :在android studio 中的build 中 make Module '  '

三: 编写service

         public void onCreate() {
                 // TODO Auto-generated method stub
                 //logE("onCreate()");
                 Log.d("Tag", "onCreate()");
                     super.onCreate();
                     mBinder = new CalculateInterfaceSub();

             }

CalculateInterfaceSub() 如下:

<pre name="code" class="java">    private static class CalculateInterfaceSub extends CalculateInterface.Stub {


        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

        }

       //读者可以对该方法进行拓展,为了简单我引入了两个int 变量
        public double doCalculate(int a, int b) throws RemoteException {

            // TODO Auto-generated method stub
            Log.e("Tag", "远程计算中");
            double answer = a + b;
            return answer;
        }


    };


 

四:Manifest 中对service 的注册:

<service android:name="com.yiran.push.service.aidlService"  android:process=":remote" >
            <intent-filter>
                <action android:name="yiran.aidlService" /><!--留意name ,客户端调用时需要 -->
            </intent-filter>
        </service>


 五: 这一步开始了客户端的编写 : 最好直接将aidl 文件夹直接复制到main 下面,自己键要确保包名一样。

 六: Layout 不过多做解释,两个可编辑文本框,填两个加数;一个按钮;一个文本框用来显示答案

七:直接上activity的代码吧,核心的部分都有注释。

public class MainActivity extends AppCompatActivity {
    EditText  editText1 ;
    EditText  editText2 ;
    Button btn ;
    TextView textView;

    private   CalculateInterface mService;
    private ServiceConnection myConn;





    private ServiceConnection mServiceConnection ;

    private class MyConn implements ServiceConnection {

        private  MyConn(){
            Log.d("Tag","MyConn");
        }


        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = CalculateInterface.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

            Log.d("Tag","onServiceDisconnected");
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText1 = (EditText)findViewById(R.id.editText);
        editText2 = (EditText)findViewById(R.id.editText2);
        textView = (TextView)findViewById(R.id.textView);
        btn = (Button)findViewById(R.id.button);

        myConn = new MyConn();

        Intent intent=new Intent();
        intent.setAction("yiran.aidlService");//配置文件中的service action name
        intent.setPackage("com.yiran.push.aidlserver");// aidl 所在的package
        boolean flag = bindService(intent, myConn, BIND_AUTO_CREATE);
        Log.d("Tag", String.valueOf(flag));

        btn.setOnClickListener(new View.OnClickListener(){

                      public void onClick(View v) {
                              // TODO Auto-generated method stub
                               Log.d("Tag","开始运算");
                               try {
                                   int  num1 = Integer.parseInt(editText1.getText().toString());
                                   int  num2 = Integer.parseInt(editText2.getText().toString());
                                   if(null==mService){
                                       Log.d("Tag","mService is null");
                                   }
                                   double answer = mService.doCalculate(num1,1);
                                   textView.setTextColor(Color.BLUE);
                                   textView.setText(String.valueOf(answer));
                                  } catch (RemoteException e) {

                                 }
                        }

        });



    }
}













  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值