33import android .graphics .Rect ;
44import android .support .v7 .app .AppCompatActivity ;
55import android .os .Bundle ;
6+ import android .util .Log ;
67import android .view .View ;
78import android .widget .Toast ;
89
2122import io .reactivex .android .schedulers .AndroidSchedulers ;
2223import 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