Android高级开发AIDL进程间通信之SDK jar包提供

SDKService

public class SDKService extends Service {

    private String TAG = "TEST";
    @Override
    public void onCreate() {
        super.onCreate();
        LogUtils.e(TAG,">>onCreate APK: ");

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        LogUtils.e(TAG,">>onStartCommand");
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        LogUtils.e(TAG,">>onDestroy");
        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        LogUtils.e(TAG,">>onBind");
        if (intent.getAction() == "com.suchao.demo.book"){
            HttpRequestManager.getInstance(CPSDKService.this);
            HttpRequestManager httpRequestManager = new HttpRequestManager(CPSDKService.this);
            return mRATrafficVioclationBinder;
        }else if(intent.getAction() == "com.suchao.demo.people"){
            HttpRequestSemanticParseManager.getInstance(CPSDKService.this);
            HttpRequestSemanticParseManager semanticParseManager = new HttpRequestSemanticParseManager(CPSDKService.this);
            return  mSemanticParseBinder;
        }else {
            return null;
        }
    }

    //
    private IBookSDKService.Stub mBookBinder = new IBookSDKService.Stub() {
        @Override
        public void requestAllProvinceList(int requestId, IRATrafficViolationSDKCallBack callBack) throws RemoteException {
            HttpBookRequestManager.getInstance(SDKService.this).requestAllProvinceList(1, callBack);
        }
    };
    //
    private IPeopleSDKService.Stub mPeopleBinder = new IPeopleSDKService.Stub() {
        @Override
        public void voiceSemanticParse(String userId, String vin, String voiceContent, IVoiceContentCallBack callBack) throws RemoteException {
            HttpPeopleRequestManager.getInstance(SDKService.this).voiceSemanticParse(userId, vin, voiceContent, callBack);
        }
    };
 
// IRATrafficVioclationSDKService.aidl
package com.reachauto.vspvehicle.ratrafficviolation;
import com.reachauto.vspvehicle.ratrafficviolation.IRATrafficViolationSDKCallBack;

interface IRATrafficVioclationSDKService {
    void registerNTSC(IRATrafficViolationSDKCallBack callBack);
    void requestAllProvinceList(int requestId, IRATrafficViolationSDKCallBack callBack);
    void requestCityList(int requestId, String var1, IRATrafficViolationSDKCallBack callBack);
    void requestViolationList(int requestId, String carInfoBeanJsonStr, IRATrafficViolationSDKCallBack callBack);
    void queryDriversLicenceScore(int requestId, String licenseInfoBeanJsonStr, IRATrafficViolationSDKCallBack callBack);

}

// IRATrafficViolationSDKCallBack.aidl
package com.reachauto.vspvehicle.ratrafficviolation;

interface IRATrafficViolationSDKCallBack {
    void ntscCallback(String code);
    void responseProvince(String requestId, String errorCode, String jsonStr);
    void responseCity(String requestId, String errorCode, String jsonStr);
    void responseViolation(String requestId, String errorCode, String jsonStr);
    void responseScoreInfo(String requestId, String errorCode, String jsonStr);
}

public class HttpRequestManager{

    private Context context;
    private static HttpRequestManager raTrafficViolationSDK;
    private static final String app = "raTrafficViolation-Service";

    public static HttpRequestManager getInstance(Context context){
        if (raTrafficViolationSDK == null){
            synchronized (HttpRequestManager.class){
                if (raTrafficViolationSDK == null){
                    raTrafficViolationSDK = new HttpRequestManager(context);
                }
            }
        }
        return raTrafficViolationSDK;
    }
    public HttpRequestManager(Context context){
        this.context = context;
    }
    
    //3.2.1请求全部省份列表
    public void requestAllProvinceList(final int requestId, final IRATrafficViolationSDKCallBack callBack){
        Map map = new HashMap();
        LogUtils.e(app  + "RequestUtil>>>requestAllProvinceList");
        String finalUrl = NetWorkUrlConstants.requestAllProvinceList_URL;
        RequsestNet.basePOSTRequest(finalUrl, map, new IHttpCallback() {
            @Override
            public void onFailure(String errorCode, String errorMessage) {
                try {
                    callBack.responseProvince(String.valueOf(requestId), errorCode, errorMessage);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onResponse(String data, String finalUrl) throws IOException {
                try {
                    callBack.responseProvince(String.valueOf(requestId),"000000", data);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
    }
 }
public class RATrafficViolationSDK{

    private Context context;
    private RATrafficViolationSDKCallBack raTrafficViolationSDKCallBack;
    private static RATrafficViolationSDK raTrafficViolationSDK;
    private IRATrafficVioclationSDKService mService;
    private static final String app = "raTrafficViolation_client";
    private static String TAG = "NSR_VSP_CP";
    private IRATrafficViolationSDKCallBackImpl mIRATrafficViolationSDKCallBackImpl;
    private static RATrafficViolationSDK mInstance;


    //单利对象初始化
    public static RATrafficViolationSDK getInstance(Context context, String vin){
        if (mInstance == null){
            synchronized (RATrafficViolationSDK.class){
                if (mInstance == null){
                    mInstance = new RATrafficViolationSDK(context);
                    CommonConstants.VIN = vin;
                }

            }
        }

        return mInstance;
    }
    //上下文
    public RATrafficViolationSDK(Context context){
        this.context = context;
    }

    //服务端链接客户端应答
    private ServiceConnection mServiceConn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            LogUtils.e(TAG, app + " >>> ServiceConnected Successful");
            mService = IRATrafficVioclationSDKService.Stub.asInterface(service);
            if (mService != null) {
                LogUtils.e(TAG,   app + "CPService服务链接成功");
            
            } else {
                LogUtils.e(TAG, app + " >>>>CPService服务链接失败 mService no connect");
            }


        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            LogUtils.e(TAG,  app + "ServiceConnected Failed");
        }
    };

    //初始化客服端绑定服务
    public void InitService(Context context){
        Intent intent = new Intent();
        intent.setAction(CommonConstants.AIDL_ACTION);
        intent.setPackage(CommonConstants.AIDL_PACKAGE);
        context.getApplicationContext().bindService(intent, mServiceConn, BIND_AUTO_CREATE);
        //启动服务
        LogUtils.e(TAG,  app + " >>> 客户端 启动服务");

    }

    //客户端注册回调方法
    public void setRaTrafficViolationSDKCallBack(RATrafficViolationSDKCallBack raTrafficViolationSDKCallBack) {
        this.raTrafficViolationSDKCallBack = raTrafficViolationSDKCallBack;
        //初始化注册服务端回调方法
        mIRATrafficViolationSDKCallBackImpl = new IRATrafficViolationSDKCallBackImpl();
        //初始化服务
        LogUtils.e(TAG,  app + "调用初始化服务方法");
        InitService(context);
    }

    //客户端请求服务端Service发起数据请求

    //3.2.1请求全部省份列表
    public void requestAllProvinceList(final int requestId) throws NullPointerException{
        try {
            if (mService == null){
                LogUtils.e(TAG,  app + "jar Request 接口》》》 mService = :" + mService);
                throw new NullPointerException("CPService is not connect");
            }
            mService.requestAllProvinceList(requestId, mIRATrafficViolationSDKCallBackImpl);
        } catch (RemoteException e) {
            e.printStackTrace();

        }
    }
  
    //服务端Service的回调数据向客户端
    class IRATrafficViolationSDKCallBackImpl extends IRATrafficViolationSDKCallBack.Stub{

   

        @Override
        public void responseProvince(String requestId, String errorCode, String jsonStr) throws RemoteException {
            RequestResult requestResult = new RequestResult();
            if (errorCode.equals("000000")){
                ProvinceListDO provinceListDO = new ProvinceListDO();
                Gson gson = new Gson();
                provinceListDO = gson.fromJson(jsonStr, ProvinceListDO.class);
                ArrayList<ProvinceBean> provinceBeanArrayList = new ArrayList<ProvinceBean>();
                if (provinceListDO.getResponseBody() != null){
                    for (ProvinceListDO.ResponseBodyBean bean:
                            provinceListDO.getResponseBody()) {
                        ProvinceBean provinceBean = new ProvinceBean();
                        provinceBean.setProvince_code(bean.getProvinceCode());
                        provinceBean.setProvince_abbr(bean.getProvinceAbbr());
                        provinceBean.setProvince(bean.getProvince());
                        provinceBeanArrayList.add(provinceBean);
                    }
                    requestResult.setResponseTime(String.valueOf(provinceListDO.getResponseTime()));
                }
                requestResult.setT(provinceBeanArrayList);
                requestResult.setStatus("000000");
                requestResult.setRequestId(String.valueOf(requestId));
                raTrafficViolationSDKCallBack.responseProvince(requestResult);
            }else {
                requestResult.setStatus(errorCode);
                requestResult.setMsg(jsonStr);
                requestResult.setRequestId(String.valueOf(requestId));
                raTrafficViolationSDKCallBack.responseProvince(requestResult);
            }
        }
   }

你可能感兴趣的:(Android)