美文网首页
AIDL使用流程

AIDL使用流程

作者: 孤独的根号十二 | 来源:发表于2018-12-24 11:39 被阅读0次

1.创建aidl文件

创建aidl.png

2.编写aidl文件,定义方法,跟普通的接口方法一样

3.点击导航栏里面的Build----》Make Project

可以看到在 project目录下, app--》build--》generated--》source--》aidl生成一个文件,里面保存生成的代码

4.编写service

在onBind中返回自定义的Binderd对象,自定义的Binder类继承生成接口的内部类,自己实现接口的方法


service代码.png

5 绑定service

通过回调方法得到IMyAidlInterface对象,调用里面的方法,实现跨进程通信

val intent=Intent()
intent.component=ComponentName("com.example.administrator.aidldemo","com.example.administrator.aidldemo.MyService")
      bindService(intent,object:ServiceConnection{
          override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
              iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service)
          }
          override fun onServiceDisconnected(name: ComponentName?){

          }
      },BIND_AUTO_CREATE)

附 manifest

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".MyService"
            android:enabled="true"
            android:exported="true"
            android:process=":message"
            >
        </service>
    </application>

相关文章

网友评论

      本文标题:AIDL使用流程

      本文链接:https://www.haomeiwen.com/subject/sbxukqtx.html