| | |
| | | package com.anyun.exam.lib; |
| | | |
| | | import android.content.ComponentName; |
| | | import android.content.Context; |
| | | import android.content.Intent; |
| | | import android.content.ServiceConnection; |
| | | import android.os.IBinder; |
| | | import android.os.RemoteException; |
| | | import android.util.Log; |
| | | |
| | | /** |
| | |
| | | * 邮箱:632393724@qq.com |
| | | * All Rights Saved! Chongqing AnYun Tech co. LTD |
| | | */ |
| | | class AYSdk { |
| | | public class AYSdk implements IAYExamInterface{ |
| | | |
| | | private static final String TAG = "AYSDK"; |
| | | |
| | | private IAYExamListener mCallback; |
| | | //远程服务接口 |
| | | private IRemoteInterface remoteInterface; |
| | | //安运驾考系统 sdk 单例模式 |
| | | private volatile static AYSdk ourInstance = null; |
| | | private Context mContext; |
| | | private MyServiceConn conn; |
| | | |
| | | static AYSdk getInstance() { |
| | | public static AYSdk getInstance() { |
| | | if (ourInstance == null){ |
| | | synchronized (AYSdk.class){ |
| | | if (ourInstance == null){ |
| | |
| | | mContext.bindService(intent,conn,Context.BIND_AUTO_CREATE); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 解绑 |
| | | */ |
| | | public void uninit(){ |
| | | if (mContext != null){ |
| | | if (remoteInterface!=null && remoteInterface.asBinder().isBinderAlive()){ |
| | | try { |
| | | remoteInterface.unRegistListener(RemoteCallback); |
| | | |
| | | } catch (RemoteException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | mContext.unbindService(conn); |
| | | }else{ |
| | | Log.e(TAG,"mContext == null,please init SDK firstly"); |
| | |
| | | private AYSdk() { |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 远程服务回调通知客户端消息 |
| | | */ |
| | | IListenerInterface RemoteCallback = new IListenerInterface.Stub(){ |
| | | @Override |
| | | public void onMessageArrived(String json) throws RemoteException { |
| | | Log.d("MyServiceConn",json); |
| | | if (mCallback != null){ |
| | | mCallback.callBackMsg(json); |
| | | } |
| | | } |
| | | |
| | | |
| | | }; |
| | | |
| | | @Override |
| | | public void checkSignal(int type) { |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void add() { |
| | | if (!checkRemoteIsNull()){ |
| | | try { |
| | | remoteInterface.add(); |
| | | } catch (RemoteException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void registListener(IAYExamListener iayExamListener) { |
| | | this.mCallback = iayExamListener; |
| | | } |
| | | |
| | | /** |
| | | * 检查远程服务是否可以使用 |
| | | * @return 可以用false,不可以用true |
| | | */ |
| | | private boolean checkRemoteIsNull() { |
| | | return remoteInterface==null?true:false; |
| | | } |
| | | |
| | | class MyServiceConn implements ServiceConnection { |
| | | @Override |
| | | public void onServiceConnected(ComponentName name, IBinder service) { |
| | | remoteInterface = IRemoteInterface.Stub.asInterface(service); |
| | | try { |
| | | //设置死亡代理 |
| | | service.linkToDeath(mDeathRecipient,0); |
| | | |
| | | remoteInterface.registListener(RemoteCallback); |
| | | } catch (RemoteException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void onServiceDisconnected(ComponentName name) { |
| | | |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | /** |
| | | * Binder断裂回调 |
| | | * 如果服务端进程由于某种异常原因停止 |
| | | */ |
| | | private IBinder.DeathRecipient mDeathRecipient = new IBinder.DeathRecipient() { |
| | | @Override |
| | | public void binderDied() { |
| | | Log.e(TAG,"IBinder.DeathRecipient binderDied"); |
| | | if (remoteInterface != null){ |
| | | Log.e(TAG,"IBinder.DeathRecipient binderDied remoteInterface != null ,return"); |
| | | return; |
| | | } |
| | | remoteInterface.asBinder().unlinkToDeath(mDeathRecipient,0); |
| | | remoteInterface = null; |
| | | } |
| | | }; |
| | | |
| | | } |