From dd7c608db87a478e22d7138e9631e98033022e66 Mon Sep 17 00:00:00 2001
From: endian11 <Dana_Lee1016@126.com>
Date: 星期三, 05 六月 2019 14:22:41 +0800
Subject: [PATCH] RemoteService aidl 回调搭架子;app依赖lib

---
 lib/src/main/AndroidManifest.xml                             |    7 ++
 lib/src/main/aidl/com/anyun/exam/lib/IListenerInterface.aidl |   12 ++++
 lib/src/main/java/com/anyun/exam/lib/AYSdk.java              |   32 ++++++++++
 lib/src/main/aidl/com/anyun/exam/lib/IRemoteInterface.aidl   |   16 +++++
 app/build.gradle                                             |    1 
 lib/src/main/java/com/anyun/exam/lib/MyServiceConn.java      |   33 +++++++++++
 lib/src/main/java/com/anyun/exam/lib/RemoteService.java      |   39 ++++++++++++
 7 files changed, 138 insertions(+), 2 deletions(-)

diff --git a/app/build.gradle b/app/build.gradle
index ac3b7ea..b20516c 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -33,4 +33,5 @@
     testImplementation 'junit:junit:4.12'
     androidTestImplementation 'com.android.support.test:runner:1.0.2'
     androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
+    implementation project(path: ':lib')
 }
diff --git a/lib/src/main/AndroidManifest.xml b/lib/src/main/AndroidManifest.xml
index 3b7edbe..270fa4d 100644
--- a/lib/src/main/AndroidManifest.xml
+++ b/lib/src/main/AndroidManifest.xml
@@ -1,2 +1,7 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
-    package="com.anyun.exam.lib" />
+    package="com.anyun.exam.lib" >
+
+    <application>
+        <service android:name=".RemoteService"  android:process=":reomte" android:exported="true"/>
+    </application>
+</manifest>
diff --git a/lib/src/main/aidl/com/anyun/exam/lib/IListenerInterface.aidl b/lib/src/main/aidl/com/anyun/exam/lib/IListenerInterface.aidl
new file mode 100644
index 0000000..df1e1b2
--- /dev/null
+++ b/lib/src/main/aidl/com/anyun/exam/lib/IListenerInterface.aidl
@@ -0,0 +1,12 @@
+// IListenerlInterface.aidl
+package com.anyun.exam.lib;
+
+// Declare any non-default types here with import statements
+
+interface IListenerInterface {
+    /**
+     * Demonstrates some basic types that you can use as parameters
+     * and return values in AIDL.
+     */
+    void onMessageArrived(String json);
+}
diff --git a/lib/src/main/aidl/com/anyun/exam/lib/IRemoteInterface.aidl b/lib/src/main/aidl/com/anyun/exam/lib/IRemoteInterface.aidl
new file mode 100644
index 0000000..15d0d3e
--- /dev/null
+++ b/lib/src/main/aidl/com/anyun/exam/lib/IRemoteInterface.aidl
@@ -0,0 +1,16 @@
+// IRemoteInterface.aidl
+package com.anyun.exam.lib;
+import com.anyun.exam.lib.IListenerInterface;
+
+// Declare any non-default types here with import statements
+
+interface IRemoteInterface {
+    /**
+     * Demonstrates some basic types that you can use as parameters
+     * and return values in AIDL.
+     */
+
+            void registListener(IListenerInterface i);
+            void unRegistListener(IListenerInterface i);
+            void add();
+}
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 36f774d..389ffda 100644
--- a/lib/src/main/java/com/anyun/exam/lib/AYSdk.java
+++ b/lib/src/main/java/com/anyun/exam/lib/AYSdk.java
@@ -1,6 +1,8 @@
 package com.anyun.exam.lib;
 
 import android.content.Context;
+import android.content.Intent;
+import android.util.Log;
 
 /**
  * MyApplication2
@@ -9,8 +11,13 @@
  * All Rights Saved! Chongqing AnYun Tech co. LTD
  */
 class AYSdk {
+
+    private static final String TAG = "AYSDK";
+
     //瀹夎繍椹捐�冪郴缁� sdk 鍗曚緥妯″紡
     private  volatile static  AYSdk ourInstance = null;
+    private Context mContext;
+    private MyServiceConn conn;
 
     static AYSdk getInstance() {
         if (ourInstance == null){
@@ -23,6 +30,31 @@
         return ourInstance;
     }
 
+    /**
+     * 鍒濆鍖朣DK
+     * @param context
+     */
+    public void init(Context context){
+        this.mContext = context;
+        //bind service
+        Intent intent = new Intent(mContext,RemoteService.class);
+        conn = new MyServiceConn();
+        mContext.bindService(intent,conn,Context.BIND_AUTO_CREATE);
+    }
+
+    /**
+     * 瑙g粦
+     */
+    public void uninit(){
+        if (mContext != null){
+            mContext.unbindService(conn);
+        }else{
+            Log.e(TAG,"mContext == null锛宲lease init SDK firstly");
+            throw new RuntimeException("mContext == null锛宲lease init SDK firstly");
+        }
+    }
+
     private AYSdk() {
+
     }
 }
diff --git a/lib/src/main/java/com/anyun/exam/lib/MyServiceConn.java b/lib/src/main/java/com/anyun/exam/lib/MyServiceConn.java
new file mode 100644
index 0000000..92f247e
--- /dev/null
+++ b/lib/src/main/java/com/anyun/exam/lib/MyServiceConn.java
@@ -0,0 +1,33 @@
+package com.anyun.exam.lib;
+
+import android.content.ComponentName;
+import android.content.ServiceConnection;
+import android.os.IBinder;
+
+/**
+ * MyApplication2
+ * Created by lzw on 2019/6/5. 13:35:18
+ * 閭锛�632393724@qq.com
+ * All Rights Saved! Chongqing AnYun Tech co. LTD
+ */
+public class MyServiceConn implements ServiceConnection {
+    @Override
+    public void onServiceConnected(ComponentName name, IBinder service) {
+
+    }
+
+    @Override
+    public void onServiceDisconnected(ComponentName name) {
+
+    }
+
+    @Override
+    public void onBindingDied(ComponentName name) {
+
+    }
+
+    @Override
+    public void onNullBinding(ComponentName name) {
+
+    }
+}
diff --git a/lib/src/main/java/com/anyun/exam/lib/RemoteService.java b/lib/src/main/java/com/anyun/exam/lib/RemoteService.java
index f69e568..1982465 100644
--- a/lib/src/main/java/com/anyun/exam/lib/RemoteService.java
+++ b/lib/src/main/java/com/anyun/exam/lib/RemoteService.java
@@ -3,7 +3,10 @@
 import android.app.Service;
 import android.content.Intent;
 import android.os.IBinder;
+import android.os.RemoteCallbackList;
+import android.os.RemoteException;
 import android.support.annotation.Nullable;
+import android.util.Log;
 
 /**
  * MyApplication2
@@ -12,9 +15,43 @@
  * All Rights Saved! Chongqing AnYun Tech co. LTD
  */
 public class RemoteService extends Service {
+
+    private static final String TAG = "RemoteService";
+    private RemoteCallbackList<IListenerInterface> mListenerList = new RemoteCallbackList();
+    private IRemoteInterface.Stub iRemoteInterface = new IRemoteInterface.Stub(){
+        @Override
+        public void registListener(IListenerInterface i) throws RemoteException {
+            //娉ㄥ唽鍥炶皟鏂规硶
+            mListenerList.register(i);
+        }
+
+        @Override
+        public void unRegistListener(IListenerInterface i) throws RemoteException {
+            mListenerList.unregister(i);
+        }
+
+        @Override
+        public void add() throws RemoteException {
+
+        }
+
+
+    };
     @Nullable
     @Override
     public IBinder onBind(Intent intent) {
-        return null;
+        return iRemoteInterface;
+    }
+
+    @Override
+    public void onCreate() {
+        super.onCreate();
+        Log.i(TAG,"onCreate()");
+    }
+
+    @Override
+    public void onDestroy() {
+        super.onDestroy();
+        Log.i(TAG,"onDestroy");
     }
 }

--
Gitblit v1.8.0