Skip to content

Commit 3c24ed5

Browse files
committed
#4 Bugfix: Improve the sample code, create remote service after AndLinker bind to the service.
1 parent bf1e577 commit 3c24ed5

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

sample/src/main/java/com/example/andlinker/BindingActivity.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.graphics.Rect;
44
import android.support.v7.app.AppCompatActivity;
55
import android.os.Bundle;
6+
import android.util.Log;
67
import android.view.View;
78
import android.widget.Toast;
89

@@ -21,8 +22,9 @@
2122
import io.reactivex.android.schedulers.AndroidSchedulers;
2223
import io.reactivex.schedulers.Schedulers;
2324

24-
public class BindingActivity extends AppCompatActivity {
25+
public class BindingActivity extends AppCompatActivity implements AndLinker.BindCallback {
2526

27+
private static final String TAG = "BindingActivity";
2628
private static final String REMOTE_SERVICE_PKG = "com.example.andlinker";
2729
public static final String REMOTE_SERVICE_ACTION = "com.example.andlinker.REMOTE_SERVICE_ACTION";
2830

@@ -44,16 +46,30 @@ protected void onCreate(Bundle savedInstanceState) {
4446
.addCallAdapterFactory(OriginalCallAdapterFactory.create()) // Basic
4547
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) // RxJava2
4648
.build();
47-
mLinker.bind();
49+
mLinker.setBindCallback(this);
4850
mLinker.registerObject(mRemoteCallback);
49-
51+
mLinker.bind();
52+
}
53+
54+
@Override
55+
public void onBind() {
56+
Log.d(TAG, "AndLinker onBind()");
5057
mRemoteService = mLinker.create(IRemoteService.class);
5158
mRemoteTask = mLinker.create(IRemoteTask.class);
5259
}
5360

61+
@Override
62+
public void onUnBind() {
63+
Log.d(TAG, "AndLinker onUnBind()");
64+
}
65+
5466
@OnClick({R.id.btn_pid, R.id.btn_basic_types, R.id.btn_call_adapter, R.id.btn_rxjava2_call_adapter,
5567
R.id.btn_callback, R.id.btn_directional, R.id.btn_oneway})
5668
public void onClick(View view) {
69+
if (mRemoteService == null || mRemoteTask == null || !mLinker.isBind()) {
70+
Log.e(TAG, "AndLinker was not bind to the service.");
71+
return;
72+
}
5773
switch (view.getId()) {
5874
case R.id.btn_pid:
5975
Toast.makeText(this, "Server pid: " + mRemoteService.getPid(), Toast.LENGTH_SHORT).show();
@@ -111,6 +127,7 @@ public void onFailure(Call<Integer> call, Throwable t) {
111127
@Override
112128
protected void onDestroy() {
113129
super.onDestroy();
130+
mLinker.setBindCallback(null);
114131
mLinker.unRegisterObject(mRemoteCallback);
115132
mLinker.unbind();
116133
}

0 commit comments

Comments
 (0)