android aidl进程间的通讯(笔记)

来源:互联网 发布:一淘与淘宝联盟 编辑:程序博客网 时间:2024/04/29 06:39

aidl: Android Interface definition language的缩写(Android接口描述语言),是为了让app间可以进行一个数据通讯的技术。

使用步骤:

1、在APP A中,创建aidl文件(该文件是app间传递数据的核心)

2、创建一个继承service的子类,在onBind函数里,返回一个aidl中的一个实例

3、在A activity里,使用bindservice来启动service,通过ServiceConnection获取aidl中的实例

4、在mainfest里面的service声明中加入隐式意图,提供给其他app访问

5、把APP A中的aidl文件连同包名复制到APP B中

6、在B activity里,使用bindservice来启动service,通过ServiceConnection获取aidl中的实例

7、实现app直接的通讯


代码说明:

1、interface ITestService {
             void toast();
      }


2、public class AService extends Service {
    int num ;

    TestServiceImpl impl = new TestServiceImpl();
    @Override
    public IBinder onBind(Intent intent) {
        return impl;
    }
       public class TestServiceImpl extends ITestService.Stub{


        @Override
        public void toast() throws RemoteException {
            num ++;
            System.out.println("!!!!!!!!!!!!!!!!  : " + num);
        }

    }

3、bindService(new Intent("com.xunle.aidlporject.service.AService"),this.serviceConnection, BIND_AUTO_CREATE);

ServiceConnection serviceConnection = new ServiceConnection() {

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

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };

4、<service android:name="com.xunle.aidlporject.service.AService" >
            <intent-filter>
                <action android:name="com.xunle.aidlporject.service.AService" />
            </intent-filter>
        </service>



0 0
原创粉丝点击