调用远程服务接口成功;
以后远程服务需要增加功能(客户端app调用),直接在IAYExamInterface里添加,然后在RemoteService实现功能;在AYSDK中调用remoteservice实现的功能;增加SDK回调给客户端app的callback,IAYEXAMListener;增减binder断裂监听
1个文件已删除
4个文件已修改
2个文件已添加
228 ■■■■ 已修改文件
app/src/main/java/safeluck/drive/evaluation/app.java 19 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
lib/src/main/aidl/com/anyun/exam/lib/IListenerInterface.aidl 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
lib/src/main/java/com/anyun/exam/lib/AYSdk.java 105 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
lib/src/main/java/com/anyun/exam/lib/IAYExamInterface.java 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
lib/src/main/java/com/anyun/exam/lib/IAYExamListener.java 13 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
lib/src/main/java/com/anyun/exam/lib/MyServiceConn.java 33 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
lib/src/main/java/com/anyun/exam/lib/RemoteService.java 42 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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);
    }
}
lib/src/main/aidl/com/anyun/exam/lib/IListenerInterface.aidl
@@ -5,6 +5,7 @@
interface IListenerInterface {
    /**
    * 远程服务给SDK的回调消息,不对外提供给APP客户端
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
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);
    }
    /**
     * 解绑
     */
    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");
@@ -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 可以用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;
        }
    };
}
lib/src/main/java/com/anyun/exam/lib/IAYExamInterface.java
New file
@@ -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);
}
lib/src/main/java/com/anyun/exam/lib/IAYExamListener.java
New file
@@ -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);
}
lib/src/main/java/com/anyun/exam/lib/MyServiceConn.java
File was deleted
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();
        }
    }
}