Skip to content

Commit 479f724

Browse files
committed
Fix unbind issue.
1 parent 5fae104 commit 479f724

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

andlinker/src/main/java/com/codezjx/andlinker/AndLinker.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
8080
* Connect to the remote service.
8181
*/
8282
public void bind() {
83+
if (isBind()) {
84+
Logger.d(TAG, "Already bind, just return.");
85+
return;
86+
}
8387
Intent intent = new Intent();
8488
if (!Utils.isStringBlank(mAction)) {
8589
intent.setAction(mAction);
@@ -95,7 +99,12 @@ public void bind() {
9599
* Disconnect from the remote service.
96100
*/
97101
public void unbind() {
102+
if (!isBind()) {
103+
Logger.d(TAG, "Already unbind, just return.");
104+
return;
105+
}
98106
mContext.unbindService(mServiceConnection);
107+
handleUnBind();
99108
}
100109

101110
/**
@@ -144,6 +153,7 @@ private ServiceConnection createServiceConnection() {
144153
return new ServiceConnection() {
145154
@Override
146155
public void onServiceConnected(ComponentName name, IBinder service) {
156+
Logger.d(TAG, "onServiceConnected:" + name + " service:" + service);
147157
mTransferService = ITransfer.Stub.asInterface(service);
148158
fireOnBind();
149159
try {
@@ -155,21 +165,26 @@ public void onServiceConnected(ComponentName name, IBinder service) {
155165

156166
@Override
157167
public void onServiceDisconnected(ComponentName name) {
158-
if (mTransferService == null) {
159-
Logger.e(TAG, "Error occur, TransferService was null when service disconnected.");
160-
fireOnUnBind();
161-
return;
162-
}
163-
try {
164-
mTransferService.unRegister(mCallback);
165-
} catch (RemoteException e) {
166-
e.printStackTrace();
167-
}
168-
mTransferService = null;
169-
fireOnUnBind();
168+
Logger.d(TAG, "onServiceDisconnected:" + name);
169+
handleUnBind();
170170
}
171171
};
172172
}
173+
174+
private void handleUnBind() {
175+
if (mTransferService == null) {
176+
Logger.e(TAG, "Error occur, TransferService was null when service disconnected.");
177+
fireOnUnBind();
178+
return;
179+
}
180+
try {
181+
mTransferService.unRegister(mCallback);
182+
} catch (RemoteException e) {
183+
e.printStackTrace();
184+
}
185+
mTransferService = null;
186+
fireOnUnBind();
187+
}
173188

174189
private void fireOnBind() {
175190
if (mBindCallback != null) {

0 commit comments

Comments
 (0)