yy1717
2020-01-06 9c13ff18dab0c35ada32a318ab6e380c9b184ffe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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;
 
import java.util.LinkedList;
 
/**
 * MyApplication2
 * Created by lzw on 2019/9/19. 18:19:48
 * 邮箱:632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */
class SvrManager {
    private static final SvrManager ourInstance = new SvrManager();
    private static final String TAG = "SvrManager";
    private MyServiceConn conn;
 
 
    private IAYExamListener mCallback;
    //远程服务接口
    private IRemoteInterface remoteInterface;
    static SvrManager getInstance() {
        return ourInstance;
    }
 
    private SvrManager() {
    }
    /**
     * 检查远程服务是否可以使用
     * @return 可以用false,不可以用true
     */
    private boolean checkRemoteIsNull() {
        return remoteInterface==null?true:false;
    }
    /**
     * 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;
        }
    };
 
    public void registerListener(IAYExamListener iayExamListener) {
        this.mCallback = iayExamListener;
    }
 
    public void add() {
        if (!checkRemoteIsNull()){
            try {
                remoteInterface.add();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }
 
    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) {
 
        }
 
 
    }
    public void bindRemoteService(Context mContext){
        Intent intent = new Intent(mContext,RemoteService.class);
        conn = new MyServiceConn();
        mContext.bindService(intent,conn, Context.BIND_AUTO_CREATE);
    }
 
    public void unBindRemoteService(Context mContext){
        if (remoteInterface!=null && remoteInterface.asBinder().isBinderAlive()){
            try {
                remoteInterface.unRegistListener(RemoteCallback);
 
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
        mContext.unbindService(conn);
    }
 
    /**
     * 远程服务回调通知客户端消息
     */
    IListenerInterface RemoteCallback = new IListenerInterface.Stub(){
        @Override
        public void onMessageArrived(String json) throws RemoteException {
 
            if (mCallback != null){
                mCallback.callBackMsg(json);
            }
        }
 
 
    };
 
}