From db2651457acfd5bfb6e3031db3e6b89eafa57a47 Mon Sep 17 00:00:00 2001
From: endian11 <Dana_Lee1016@126.com>
Date: 星期三, 05 六月 2019 16:58:41 +0800
Subject: [PATCH] 调用远程服务接口成功; 以后远程服务需要增加功能(客户端app调用),直接在IAYExamInterface里添加,然后在RemoteService实现功能;在AYSDK中调用remoteservice实现的功能;增加SDK回调给客户端app的callback,IAYEXAMListener;增减binder断裂监听

---
 lib/src/main/java/com/anyun/exam/lib/AYSdk.java |  105 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 103 insertions(+), 2 deletions(-)

diff --git a/lib/src/main/java/com/anyun/exam/lib/AYSdk.java b/lib/src/main/java/com/anyun/exam/lib/AYSdk.java
index 389ffda..659a8b5 100644
--- a/lib/src/main/java/com/anyun/exam/lib/AYSdk.java
+++ b/lib/src/main/java/com/anyun/exam/lib/AYSdk.java
@@ -1,7 +1,11 @@
 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;
 
 /**
@@ -10,16 +14,19 @@
  * 閭锛�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){
@@ -42,11 +49,21 @@
         mContext.bindService(intent,conn,Context.BIND_AUTO_CREATE);
     }
 
+
+
     /**
      * 瑙g粦
      */
     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锛宲lease init SDK firstly");
@@ -57,4 +74,88 @@
     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 鍙互鐢╢alse锛屼笉鍙互鐢╰rue
+     */
+    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 {
+                //璁剧疆姝讳骸浠g悊
+             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;
+        }
+    };
+
 }

--
Gitblit v1.8.0