android aidl通信实例

AIDL是Android Interface definition language的缩写
下面是aidl通信的实例代码
1、aidl接口定义
(1)callback

package com.test.aidltest;
interface InfTestCallback {
    void onAidlTestCallback(String msg);
}

(2)Service

package com.test.aidltest;
import com.test.aidltest.InfTestCallback;
interface InfTestService {
    void aidlInit(in InfTestCallback callback);
    void aidlUnInit(in InfTestCallback callback);
}

2、Servier端
(1)Service

package com.study.aidlserver;
public class AidlTestService extends Service {
    private String TAG = "AidlTestServer";
    public final RemoteCallbackList<InfTestCallback> mCallbacks = new RemoteCallbackList<InfTestCallback>();
    private InfTestService aidlTestServer = new InfTestService.Stub() {
        @Override
        public void aidlInit(in InfTestCallback callback) throws RemoteException {
            Log.d(TAG, "aidl init ...");

            if (null == callback) {
                Log.e(TAG, "register Callback is null !");
                return;
            }

            mCallbacks.register(callback); //注册回调
            callback.onAidlTestCallback("aidl init success");

            Log.d(TAG, "aidl init end");
            return;
        }

        @Override
        public void aidlUnInit(in InfTestCallback callback) throws RemoteException {
            Log.d(TAG, "aidl uninit ...");
            mCallbacks.unregister(callback);
            Log.d(TAG, "aidl uninit end");
        }
    };

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return (IBinder)aidlTestServer;
    }
}

(2)activity

package com.study.aidlserver;

public class MainActivity extends AppCompatActivity {
    public AidlTestServer aidlTestServer = null;
    private final Timer timer = new Timer();
    private TimerTask task;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (null == aidlTestServer) {
            aidlTestServer = new AidlTestServer();
        }

        task = new TimerTask() {
            @Override
            public void run() {
                if ((null != aidlTestServer.mCallbacks) && (aidlTestServer.mCallbacks.getRegisteredCallbackCount() <= 0)) {
                    synchronized (aidlTestServer.mCallbacks) {
                        aidlTestServer.mCallbacks.beginBroadcast();
                        int number = aidlTestServer.mCallbacks.getRegisteredCallbackCount();
                        for (int i = 0; i < number; i++) {
                            try {
                                if (aidlTestServer.mCallbacks.getBroadcastItem(i) == null) {
                                    continue;
                                }
                                aidlTestServer.mCallbacks.getBroadcastItem(i).onAidlTestCallback("aidl test callback");
                            } catch (DeadObjectException e) {
                                if (aidlTestServer.mCallbacks.getBroadcastItem(i) != null)
                                    aidlTestServer.mCallbacks.unregister(aidlTestServer.mCallbacks.getBroadcastItem(i));
                            } catch (RemoteException e) {
                                e.printStackTrace();
                            }
                        }
                        aidlTestServer.mCallbacks.finishBroadcast();
                    }
                }
            }
        };

        timer.schedule(task, 2000, 2000);
    }
}

3、Client端:

package com.study.aidlclient;
public class MainActivity extends AppCompatActivity {

    private InfTestService testServiceInterface = null;
    private String TAG = "AidlTestClient";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bindTestService(this);
        testServiceInterface = InfTestService.Stub.asInterface();
        if (null == testServiceInterface) {
            Log.d(TAG, "testServiceInterface is null");
            return;
        }
    }

    public ServiceConnection connection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(TAG, "onServiceConnected name "+name);
            if (null == service) {
                Log.d(TAG, "service is null");
                return;
            }

            testServiceInterface = InfTestService.Stub.asInterface(service);
            if (null == testServiceInterface) {
                Log.d(TAG, "testServiceInterface is null");
                return;
            }

            try {
                if (null != testServiceInterface) {
                    testServiceInterface.aidlInit(mInfTestCallback);
                } else {
                    Log.d(TAG, "init: testServiceInterface is null");
                }
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d(TAG, "onServiceDisconnected name "+name);
            if (null != testServiceInterface) {
                testServiceInterface.aidlUnInit(mInfTestCallback);
            } else {
                Log.d(TAG, "init: testServiceInterface is null");
            }
        }
    };

    public boolean bindTestService(Context context) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.study.aidlserver","com.study.aidlserver.AidlTestService"));
        boolean ret = context.bindService(intent, connection, Context.BIND_AUTO_CREATE);
        Log.d(TAG, "bindTestService: ");
        return ret;
    }

    public void unBindTestService(Context context) {
        context.unbindService(connection);
    }

    private InfTestCallback mInfTestCallback = new InfTestCallback.Stub() {
        @Override
        public void onAidlTestCallback(String msg) {
            Log.d(TAG, "AidlTestCallback = "+msg);
        }
    };
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值