IPC机制 : AIDL的用法

HeiMcMullan 7年前
   <p>(AS开发工具)</p>    <h2><strong>服务器端</strong></h2>    <p><strong>第一步 :创建aidl文件</strong></p>    <ul>     <li> <p>在与java文件夹同级目录下,创建文件夹 aidl ,然后在aidl文件夹下创建包 com.lyp.aidl ,再在包下创建 IMyAidlInterface.aidl 文件</p> </li>    </ul>    <p style="text-align:center"><img src="https://simg.open-open.com/show/a8c1846cafcb1cdbca032c66ce751cc4.png"></p>    <p style="text-align:center">Paste_Image.png</p>    <pre>  <code class="language-java">// IMyAidlInterface.aidl  package com.lyp.aidl;    // Declare any non-default types here with import statements    interface IMyAidlInterface {      /**       * 1.接口和方法前不用加private等访问权限的修饰符,当然也不能使用final,static修饰符       * 2.接口名称和aidl的名称要一致       * 3.aidl默认支持的类型包话java基本类型(int、long、boolean)和(String、List、Map、CharSequence),       *   使用这些类型时不需要import声明。对于List和Map中的元素类型必须是Aidl支持的类型。如果使用自定义类型       *   作为参数或返回值,自定义类型必须实现Parcelable接口       * 4.自定义类型和AIDL生成的其它接口类型在aidl描述文件中,应该显式import,即便在该类和定义的包在同一个包中       * 5.在aidl文件中所有非Java基本类型参数必须加上in、out、inout标记,以指明参数是输入参数、输出参数还是输入输出参数       */      int add(int num1, int num2);  }</code></pre>    <ul>     <li>使用as工具栏Build/make project ,重新编译 ,生成与aidl文件同名的java文件</li>    </ul>    <p style="text-align:center"><img src="https://simg.open-open.com/show/d24c7d09509609ed6c597db3012d6bf2.png"></p>    <p style="text-align:center">Paste_Image.png</p>    <p><strong>第二步 : 完成服务器端Server类</strong></p>    <pre>  <code class="language-java">package com.lyp.aidlserver;    import android.app.Service;  import android.content.Intent;  import android.os.IBinder;  import android.os.RemoteException;  import android.support.annotation.Nullable;    import com.lyp.aidl.IMyAidlInterface;      /**   * Created by ${思格斯} on 2016/10/8 0008.   */  public class AidlService extends Service {      public AidlService() {      }        @Nullable      @Override      public IBinder onBind(Intent intent) {          return mBinder;      }        //获取IMyAidlInterface.Stub对象,实现IMyAidlInterface.java中的add方法      private IBinder mBinder= new IMyAidlInterface.Stub() {          @Override          public int add(int num1, int num2) throws RemoteException {              return num1+num2;          }      };  }</code></pre>    <h2><strong>客户端</strong></h2>    <p><strong>第一步 : 将服务器端的aidl文件夹复制一份到客户端</strong></p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/ed583c9cb73cb1256958ab149136ba6c.png"></p>    <p style="text-align:center">Paste_Image.png</p>    <p><strong>第二步 : 绑定服务端Service , 获取代理对象 ,调用aidl文件自动定义的方法</strong></p>    <pre>  <code class="language-java">package com.lyp.aidlclient;    import android.content.ComponentName;  import android.content.Intent;  import android.content.ServiceConnection;  import android.os.IBinder;  import android.os.RemoteException;  import android.support.v7.app.AppCompatActivity;  import android.os.Bundle;  import android.view.View;  import android.widget.Button;  import android.widget.TextView;    import com.lyp.aidl.IMyAidlInterface;    public class MainActivity extends AppCompatActivity {      private TextView mTextView;      private Button mButton;        private IMyAidlInterface mBinder;        @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);            mTextView = (TextView) findViewById(R.id.text);          mButton= (Button) findViewById(R.id.btn);            bindService();            //调用服务端方法          mButton.setOnClickListener(new View.OnClickListener() {              @Override              public void onClick(View v) {                  try {                      int res = mBinder.add(1, 1);                      mTextView.setText(String.valueOf(res));                  } catch (RemoteException e) {                      e.printStackTrace();                  }              }          });      }        //显式绑定服务      private void bindService() {          Intent intent = new Intent("android.intent.action.AIDLSERVICE");          intent.setPackage("com.lyp.aidlserver");          bindService(intent, conn, BIND_AUTO_CREATE);      }        ServiceConnection conn = new ServiceConnection() {          @Override          public void onServiceConnected(ComponentName name, IBinder service) {              //获取远程服务代理              mBinder = IMyAidlInterface.Stub.asInterface(service);          }            @Override          public void onServiceDisconnected(ComponentName name) {              //释放资源              mBinder = null;          }      };        @Override      protected void onDestroy() {          super.onDestroy();          //解绑服务          unbindService(conn);      }  }</code></pre>    <p> </p>    <p>来自:http://www.jianshu.com/p/e84bdb4cf28a</p>    <p> </p>