Android Aidl 爱之初体验?

DomenicR65 8年前
   <p>从前有个山 山里有个庙 庙里有个和尚 。。。。。一年过去了。老和尚跟小和尚说,孩子时候下去见母老虎了,山下的女人是老虎,师傅有一个要求您要学会aidl,当时小和尚一听懵逼了,what is this?这时老和尚摸着小和尚的头说:”学会aidl你就能下山了,遇到母老虎就可以跟我通信,师傅就马上下山救您了“,</p>    <p>遇见了不要慌,就这样小和尚很不开心,因为马上要离开师傅了,不开心,宝宝不开心嘛,但是师傅掏出一本宝典 焕名”“aidl之远程助攻母老虎72式”,小和尚拿着这本宝典开开心心的下山了,就这样十年过去了,小和尚并没有看到很多母老虎,他这时又想起师傅的话,默默的流泪,我是不是找了个假师傅,好尴尬呢?好了下面直接练习宝典!开大招!</p>    <pre>  <code class="language-java">Soeasy!</code></pre>    <p><strong>首先看第一招:入门式</strong></p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/c7dd10e81f870cebc3f716ce34965bbb.gif"></p>    <p><strong>第二招:惑骗式</strong></p>    <p>先给大佬们上个图</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/9432a12b6fa3298df3e6ba2796f17850.png"></p>    <p><strong>第三招 :进攻式</strong></p>    <ol>     <li>首先在src目录下创建一个aidl目录</li>     <li>创建一个IRemoteService.aidl文件</li>     <li>支持的数据类型 aidl支持的参数类型有int long boolean float double String</li>    </ol>    <p><strong>第四招:招虎式</strong></p>    <p>这里创建一个方法进行通信</p>    <p>String baseAidl();</p>    <p><strong>第五招:训母式</strong></p>    <p>1、这里需要创建一个RemoteService类</p>    <p>2、然后绑定服务类</p>    <p>3、创建一个Stub抽象类</p>    <p>4、在清单文件AndroidMainfest.xml中注册 需要注意5.0以上系统需要设置包名 acton Name等</p>    <p>全部代码如下</p>    <pre>  <code class="language-java">package com.example.arial.dgamedetail.service;    import android.app.Service;  import android.content.ComponentName;  import android.content.Intent;  import android.content.ServiceConnection;  import android.os.IBinder;  import android.os.RemoteException;  import android.support.annotation.Nullable;  import android.support.v4.os.IResultReceiver;  import android.util.Log;    import com.example.arial.dgamedetail.IRemoteService;    /**   * Created by John on 2017/3/7.   */    public class RemoteService extends Service {      private static final String TAG = "RemoteService";          public static abstract class Stub extends IRemoteService.Stub {        }        private IRemoteService.Stub mIRemoteService = new IRemoteService.Stub() {          @Override          public String baseAidl() throws RemoteException {              return "您好,我是Kadan";          }            @Override          public IBinder asBinder() {              return super.asBinder();          }            @Override          public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {            }      };        @Nullable      @Override      public IBinder onBind(Intent intent) {          Intent intent1 = new Intent(this, RemoteService.class);          bindService(intent1, new ServiceConnection() {              @Override              public void onServiceConnected(ComponentName name, IBinder service) {                  IResultReceiver.Stub.asInterface(service);                  Log.e(TAG, "onServiceConnected: ");              }                @Override              public void onServiceDisconnected(ComponentName name) {                }          }, BIND_AUTO_CREATE);            return mIRemoteService.asBinder();      }        @Override      public boolean bindService(Intent service, ServiceConnection conn, int flags) {          return super.bindService(service, conn, flags);      }  }</code></pre>    <p>绑定服务</p>    <pre>  <code class="language-java">@Nullable      @Override      public IBinder onBind(Intent intent) {          Intent intent1 = new Intent(this, RemoteService.class);          bindService(intent1, new ServiceConnection() {              @Override              public void onServiceConnected(ComponentName name, IBinder service) {                  IResultReceiver.Stub.asInterface(service);                  Log.e(TAG, "onServiceConnected: ");              }                @Override              public void onServiceDisconnected(ComponentName name) {                }          }, BIND_AUTO_CREATE);            return mIRemoteService.asBinder();      }  @Override      public boolean bindService(Intent service, ServiceConnection conn, int flags) {          return super.bindService(service, conn, flags);      }</code></pre>    <p>清单文件中注册</p>    <pre>  <code class="language-java"><service android:name=".service.RemoteService" android:process=":remote">              <intent-filter >                  <action android:name="com.example.arial.dgamedetail.service.RemoteService"/>              </intent-filter>          </service></code></pre>    <p><strong>第六招 :放虎式</strong></p>    <p>写完之后别忘记主Activity中调用需要在onResume再次绑定服务</p>    <pre>  <code class="language-java">@Override      protected void onResume() {          super.onResume();          Intent intent=new Intent();          intent.setAction(Constant.ACTION);          //5.0以上系统需要设置           intent.setPackage(Constant.PACKAGE);          //绑定服务          bindService(intent, new ServiceConnection() {              @Override              public void onServiceConnected(ComponentName name, IBinder service) {                mIRemoteService=IRemoteService.Stub.asInterface(service);              }                @Override              public void onServiceDisconnected(ComponentName name) {                }          },BIND_AUTO_CREATE);//自动创建      }</code></pre>    <p>另外给按钮设置一个监听事件我这里使用的是xml中添加onClick方法Aidl</p>    <pre>  <code class="language-java">/**       * 设置发送消息的监听事件       * @param v       */      public void Aidl(View v)      {          //判断当前服务是否为空          try {              if(mIRemoteService!=null){                  String text=mIRemoteService.baseAidl();                  if(text!=null)                  {                      tv_aidl.setText(text);                  }              }else{                  tv_aidl.setText("不好意思没有获取到消息");              }          } catch (RemoteException e) {              e.printStackTrace();          }      }</code></pre>    <p>初始化TextView这里不再赘述看宝典吧</p>    <pre>  <code class="language-java">/**       * 初始化UI组件       */      private void initView() {          tv_aidl= (TextView) findViewById(R.id.tv_aidl);      }</code></pre>    <p>最后贴上AidlActivity完整代码</p>    <pre>  <code class="language-java">package com.example.arial.dgamedetail;    import android.content.ComponentName;  import android.content.Intent;  import android.content.ServiceConnection;  import android.os.Bundle;  import android.os.IBinder;  import android.os.RemoteException;  import android.support.annotation.Nullable;  import android.support.v7.app.AppCompatActivity;  import android.view.View;  import android.widget.TextView;    import com.example.arial.dgamedetail.constant.Constant;    /**   * Created by John on 2017/3/7.   */    public class AidlActivity extends AppCompatActivity {      //远程服务类      private IRemoteService mIRemoteService;      //显示的内容      private TextView tv_aidl;      @Override      protected void onCreate(@Nullable Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_aidl);          initView();      }        /**       * 初始化UI组件       */      private void initView() {          tv_aidl= (TextView) findViewById(R.id.tv_aidl);      }        /**       * 设置发送消息的监听事件       * @param v       */      public void Aidl(View v)      {          //判断当前服务是否为空          try {              if(mIRemoteService!=null){                  String text=mIRemoteService.baseAidl();                  if(text!=null)                  {                      tv_aidl.setText(text);                  }              }else{                  tv_aidl.setText("不好意思没有获取到消息");              }          } catch (RemoteException e) {              e.printStackTrace();          }      }        @Override      protected void onResume() {          super.onResume();          Intent intent=new Intent();          intent.setAction(Constant.ACTION);          //5.0以上系统需要设置          intent.setPackage(Constant.PACKAGE);          //绑定服务          bindService(intent, new ServiceConnection() {              @Override              public void onServiceConnected(ComponentName name, IBinder service) {                mIRemoteService=IRemoteService.Stub.asInterface(service);              }                @Override              public void onServiceDisconnected(ComponentName name) {                }          },BIND_AUTO_CREATE);//自动创建      }  }</code></pre>    <p>最后看下Constant封装</p>    <pre>  <code class="language-java">package com.example.arial.dgamedetail.constant;    /**   * Created by John on 2017/3/7.   */    public class Constant {      //包名      public static final String PACKAGE="com.example.arial.dgamedetail";      //清单文件注册的action      public static final String ACTION="com.example.arial.dgamedetail.service.RemoteService";  }</code></pre>    <p>到这里基本技术了我只是个初级的就是aidl基本使用 然后进行简单的远程通信</p>    <p>可能以后还需要跟服务端进行通信等等,其实android与银行对接用的就是这种机制,如果大家感兴趣可以自己去网上了解学习哈!</p>    <p> </p>    <p>来自:http://blog.csdn.net/qq_15950325/article/details/60756495</p>    <p> </p>