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断裂监听 --- app/src/main/java/safeluck/drive/evaluation/app.java | 19 ++++ lib/src/main/java/com/anyun/exam/lib/IAYExamInterface.java | 15 +++ /dev/null | 33 -------- lib/src/main/aidl/com/anyun/exam/lib/IListenerInterface.aidl | 1 lib/src/main/java/com/anyun/exam/lib/IAYExamListener.java | 13 +++ lib/src/main/java/com/anyun/exam/lib/AYSdk.java | 105 +++++++++++++++++++++++++ lib/src/main/java/com/anyun/exam/lib/RemoteService.java | 42 ++++++++++ 7 files changed, 191 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/safeluck/drive/evaluation/app.java b/app/src/main/java/safeluck/drive/evaluation/app.java index bf4e141..ee67d0f 100644 --- a/app/src/main/java/safeluck/drive/evaluation/app.java +++ b/app/src/main/java/safeluck/drive/evaluation/app.java @@ -4,6 +4,9 @@ import android.support.annotation.NonNull; import android.util.Log; +import com.anyun.exam.lib.AYSdk; +import com.anyun.exam.lib.IAYExamListener; + import me.yokeyword.fragmentation.Fragmentation; import me.yokeyword.fragmentation.helper.ExceptionHandler; @@ -13,7 +16,7 @@ * 閭锛�632393724@qq.com * All Rights Saved! Chongqing AnYun Tech co. LTD */ -public class app extends Application { +public class app extends Application implements IAYExamListener { private static final String TAG = "app"; @Override @@ -28,5 +31,19 @@ } }) .install(); + + AYSdk.getInstance().init(getApplicationContext()); + AYSdk.getInstance().registListener(this); + } + + @Override + public void onTerminate() { + super.onTerminate(); + AYSdk.getInstance().uninit(); + } + + @Override + public void callBackMsg(String json) { + Log.d(TAG,"鏀跺埌=="+json); } } diff --git a/lib/src/main/aidl/com/anyun/exam/lib/IListenerInterface.aidl b/lib/src/main/aidl/com/anyun/exam/lib/IListenerInterface.aidl index df1e1b2..0bf8dbb 100644 --- a/lib/src/main/aidl/com/anyun/exam/lib/IListenerInterface.aidl +++ b/lib/src/main/aidl/com/anyun/exam/lib/IListenerInterface.aidl @@ -5,6 +5,7 @@ interface IListenerInterface { /** + * 杩滅▼鏈嶅姟缁橲DK鐨勫洖璋冩秷鎭紝涓嶅澶栨彁渚涚粰APP瀹㈡埛绔� * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ 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; + } + }; + } diff --git a/lib/src/main/java/com/anyun/exam/lib/IAYExamInterface.java b/lib/src/main/java/com/anyun/exam/lib/IAYExamInterface.java new file mode 100644 index 0000000..24eddf7 --- /dev/null +++ b/lib/src/main/java/com/anyun/exam/lib/IAYExamInterface.java @@ -0,0 +1,15 @@ +package com.anyun.exam.lib; + +/** + * 杩滅▼鏈嶅姟鎵�鎻愪緵鐨勬柟娉� + * MyApplication2 + * Created by lzw on 2019/6/5. 15:37:20 + * 閭锛�632393724@qq.com + * All Rights Saved! Chongqing AnYun Tech co. LTD + */ +public interface IAYExamInterface { + //浠ヤ笅鏂规硶鍙槸绀轰緥鏂规硶锛岀敓浜х幆澧冮渶瑕佸垹闄� + void checkSignal(int type); + void add(); + void registListener(IAYExamListener iayExamListener); +} diff --git a/lib/src/main/java/com/anyun/exam/lib/IAYExamListener.java b/lib/src/main/java/com/anyun/exam/lib/IAYExamListener.java new file mode 100644 index 0000000..cde1ac5 --- /dev/null +++ b/lib/src/main/java/com/anyun/exam/lib/IAYExamListener.java @@ -0,0 +1,13 @@ +package com.anyun.exam.lib; + +/** + * + * SDK鍥炶皟缁欏鎴风鐨勬秷鎭� + * MyApplication2 + * Created by lzw on 2019/6/5. 15:43:48 + * 閭锛�632393724@qq.com + * All Rights Saved! Chongqing AnYun Tech co. LTD + */ +public interface IAYExamListener { + void callBackMsg(String json); +} diff --git a/lib/src/main/java/com/anyun/exam/lib/MyServiceConn.java b/lib/src/main/java/com/anyun/exam/lib/MyServiceConn.java deleted file mode 100644 index 92f247e..0000000 --- a/lib/src/main/java/com/anyun/exam/lib/MyServiceConn.java +++ /dev/null @@ -1,33 +0,0 @@ -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 1982465..f525584 100644 --- a/lib/src/main/java/com/anyun/exam/lib/RemoteService.java +++ b/lib/src/main/java/com/anyun/exam/lib/RemoteService.java @@ -8,6 +8,8 @@ import android.support.annotation.Nullable; import android.util.Log; +import java.util.concurrent.atomic.AtomicBoolean; + /** * MyApplication2 * Created by lzw on 2019/6/5. 13:22:46 @@ -17,7 +19,11 @@ public class RemoteService extends Service { private static final String TAG = "RemoteService"; + //鏈嶅姟鏄惁閿�姣� + private AtomicBoolean mIsServiceDestroyed = new AtomicBoolean(false); private RemoteCallbackList<IListenerInterface> mListenerList = new RemoteCallbackList(); + private IListenerInterface mListener; + private int msgId = 0; private IRemoteInterface.Stub iRemoteInterface = new IRemoteInterface.Stub(){ @Override public void registListener(IListenerInterface i) throws RemoteException { @@ -32,7 +38,7 @@ @Override public void add() throws RemoteException { - + Log.i(TAG,"瀹㈡埛绔皟鐢ㄦ湇鍔$鏂规硶"); } @@ -47,11 +53,45 @@ public void onCreate() { super.onCreate(); Log.i(TAG,"onCreate()"); + new Thread(new Worker()).start(); + } + + private class Worker implements Runnable{ + + @Override + public void run() { + while (!mIsServiceDestroyed.get()){ + try { + Thread.sleep(4*1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + onMessageArrived(String.format("娑堟伅ID锛�%d,璇锋敞鎰忔煡鏀�",msgId++)); + } + + } } @Override public void onDestroy() { super.onDestroy(); Log.i(TAG,"onDestroy"); + mIsServiceDestroyed.set(true); + } + + private void onMessageArrived(String json){ + int N = mListenerList.getRegisteredCallbackCount(); + for (int i = 0; i < N; i++) { + mListenerList.beginBroadcast(); + mListener = mListenerList.getBroadcastItem(i); + if (mListener != null){ + try { + mListener.onMessageArrived(json); + } catch (RemoteException e) { + e.printStackTrace(); + } + } + mListenerList.finishBroadcast(); + } } } -- Gitblit v1.8.0