1616import android .support .annotation .NonNull ;
1717import android .support .annotation .Nullable ;
1818import android .text .TextUtils ;
19+ import android .util .Log ;
1920
2021import java .util .ArrayList ;
2122import java .util .LinkedList ;
@@ -44,11 +45,11 @@ public class MessengerUtils {
4445
4546 public static void init () {
4647 if (isMainProcess ()) {
47- LogUtils .e ();
48+ LogUtils .e ("init " + getCurrentProcessName () );
4849 Intent intent = new Intent (Utils .getApp (), ServerService .class );
4950 Utils .getApp ().startService (intent );
5051 } else {
51- LogUtils .e ();
52+ LogUtils .e ("init " + getCurrentProcessName () );
5253 registerClient ();
5354 }
5455 }
@@ -79,14 +80,14 @@ public static void unsubscribe(String key) {
7980 public static void post (@ NonNull String key , @ NonNull Bundle data ) {
8081 data .putString (KEY_STRING , key );
8182 if (sLocalClient != null ) {
82- sLocalClient .sendMsg (data );
83+ sLocalClient .sendMsg2Server (data );
8384 } else {
8485 Intent intent = new Intent (Utils .getApp (), ServerService .class );
8586 intent .putExtras (data );
8687 Utils .getApp ().startService (intent );
8788 }
8889// if (otherAppClient != null) {
89- // otherAppClient.sendMsg (data);
90+ // otherAppClient.sendMsg2Server (data);
9091// }
9192 }
9293
@@ -149,16 +150,18 @@ public void onServiceConnected(ComponentName name, IBinder service) {
149150 try {
150151 mServer .send (subscribeMsg );
151152 } catch (RemoteException e ) {
152- e . printStackTrace ( );
153+ Log . e ( "MessengerUtils" , "onServiceConnected: " , e );
153154 }
154- sendCachedMsg ();
155+ sendCachedMsg2Server ();
155156 }
156157
157158 @ Override
158159 public void onServiceDisconnected (ComponentName name ) {
159- LogUtils . d ( " client service disconnected" , name );
160+ Log . w ( "MessengerUtils" , " client service disconnected:" + name );
160161 mServer = null ;
161- // connect();
162+ if (!bind ()) {
163+ Log .e ("MessengerUtils" , "client service rebind failed: " + name );
164+ }
162165 }
163166 };
164167
@@ -181,26 +184,37 @@ void unbind() {
181184 Utils .getApp ().unbindService (mConn );
182185 }
183186
184- void sendMsg (Bundle bundle ) {
185- if (mServer != null ) {
186- sendCachedMsg ();
187- Message msg = Message .obtain (mHandler , SEND_MSG_TO_TARGET );
188- msg .setData (bundle );
189- msg .replyTo = mClient ;
190- try {
191- mServer .send (msg );
192- } catch (RemoteException e ) {
193- e .printStackTrace ();
194- }
195- } else {
187+ void sendMsg2Server (Bundle bundle ) {
188+ if (mServer == null ) {
196189 mCached .addFirst (bundle );
190+ LogUtils .d ("save the bundle" , bundle );
191+ } else {
192+ sendCachedMsg2Server ();
193+ if (!send2Server (bundle )) {
194+ mCached .addFirst (bundle );
195+ }
197196 }
198197 }
199198
200- private void sendCachedMsg () {
201- for (int i = mCached .size () - 1 ; i >= 0 ; i --) {
202- sendMsg (mCached .get (i ));
203- mCached .remove (i );
199+ private void sendCachedMsg2Server () {
200+ if (mCached .isEmpty ()) return ;
201+ for (int i = mCached .size () - 1 ; i >= 0 ; --i ) {
202+ if (send2Server (mCached .get (i ))) {
203+ mCached .remove (i );
204+ }
205+ }
206+ }
207+
208+ private boolean send2Server (Bundle bundle ) {
209+ Message msg = Message .obtain (mHandler , SEND_MSG_TO_TARGET );
210+ msg .setData (bundle );
211+ msg .replyTo = mClient ;
212+ try {
213+ mServer .send (msg );
214+ return true ;
215+ } catch (RemoteException e ) {
216+ Log .e ("MessengerUtils" , "send2Server: " , e );
217+ return false ;
204218 }
205219 }
206220 }
0 commit comments