77import android .widget .Toast ;
88
99import com .codezjx .andlinker .AndLinker ;
10+ import com .codezjx .andlinker .Call ;
11+ import com .codezjx .andlinker .Callback ;
12+ import com .codezjx .andlinker .adapter .OriginalCallAdapterFactory ;
13+ import com .codezjx .andlinker .adapter .rxjava2 .RxJava2CallAdapterFactory ;
14+ import com .codezjx .andlinker .annotation .ClassName ;
15+ import com .codezjx .andlinker .annotation .MethodName ;
16+
17+ import java .util .List ;
1018
1119import butterknife .ButterKnife ;
1220import butterknife .OnClick ;
21+ import io .reactivex .Observable ;
22+ import io .reactivex .android .schedulers .AndroidSchedulers ;
23+ import io .reactivex .schedulers .Schedulers ;
1324
1425public class BindingActivity extends AppCompatActivity {
1526
@@ -18,6 +29,7 @@ public class BindingActivity extends AppCompatActivity {
1829
1930 private AndLinker mLinker ;
2031 private IRemoteService mRemoteService ;
32+ private IRemoteTask mRemoteTask ;
2133
2234 @ Override
2335 protected void onCreate (Bundle savedInstanceState ) {
@@ -28,14 +40,20 @@ protected void onCreate(Bundle savedInstanceState) {
2840 mLinker = new AndLinker .Builder (this )
2941 .packageName (REMOTE_SERVICE_PKG )
3042 .action (REMOTE_SERVICE_ACTION )
43+ // Specify the callback executor by yourself
44+ //.addCallAdapterFactory(OriginalCallAdapterFactory.create(callbackExecutor))
45+ .addCallAdapterFactory (OriginalCallAdapterFactory .create ()) // Basic
46+ .addCallAdapterFactory (RxJava2CallAdapterFactory .create ()) // RxJava2
3147 .build ();
3248 mLinker .bind ();
3349 mLinker .registerObject (mRemoteCallback );
3450
3551 mRemoteService = mLinker .create (IRemoteService .class );
52+ mRemoteTask = mLinker .create (IRemoteTask .class );
3653 }
3754
38- @ OnClick ({R .id .btn_pid , R .id .btn_basic_types , R .id .btn_callback , R .id .btn_directional , R .id .btn_oneway })
55+ @ OnClick ({R .id .btn_pid , R .id .btn_basic_types , R .id .btn_call_adapter , R .id .btn_rxjava2_call_adapter ,
56+ R .id .btn_callback , R .id .btn_directional , R .id .btn_oneway })
3957 public void onClick (View view ) {
4058 switch (view .getId ()) {
4159 case R .id .btn_pid :
@@ -45,6 +63,32 @@ public void onClick(View view) {
4563 case R .id .btn_basic_types :
4664 mRemoteService .basicTypes (1 , 2L , true , 3.0f , 4.0d , "str" );
4765 break ;
66+ case R .id .btn_call_adapter :
67+ Call <Integer > call = mRemoteTask .remoteCalculate (10 , 20 );
68+
69+ // Synchronous Request
70+ // int result = call.execute();
71+ // Log.d("BindingActivity", "remoteCalculate() result:" + result);
72+
73+ // Asynchronous Request
74+ call .enqueue (new Callback <Integer >() {
75+ @ Override
76+ public void onResponse (Call <Integer > call , Integer response ) {
77+ Toast .makeText (BindingActivity .this , "remoteCalculate() onResponse: " + response , Toast .LENGTH_SHORT ).show ();
78+ }
79+
80+ @ Override
81+ public void onFailure (Call <Integer > call , Throwable t ) {
82+ Toast .makeText (BindingActivity .this , "remoteCalculate() failure: " + t .getMessage (), Toast .LENGTH_SHORT ).show ();
83+ }
84+ });
85+ break ;
86+ case R .id .btn_rxjava2_call_adapter :
87+ mRemoteTask .getDatas ()
88+ .subscribeOn (Schedulers .io ())
89+ .observeOn (AndroidSchedulers .mainThread ())
90+ .subscribe (datas -> Toast .makeText (this , "getDatas() return: " + datas , Toast .LENGTH_LONG ).show ());
91+ break ;
4892 case R .id .btn_callback :
4993 mRemoteService .registerCallback (mRemoteCallback );
5094 break ;
@@ -78,4 +122,19 @@ public void onValueChange(int value) {
78122 Toast .makeText (BindingActivity .this , "Server callback value: " + value , Toast .LENGTH_SHORT ).show ();
79123 }
80124 };
125+
126+
127+ /**
128+ * Copy the original interface, wrap the return type of the method, keep the original @ClassName and @MethodName.
129+ */
130+ @ ClassName ("com.example.andlinker.IRemoteTask" )
131+ public interface IRemoteTask {
132+
133+ @ MethodName ("remoteCalculate" )
134+ Call <Integer > remoteCalculate (int a , int b );
135+
136+ @ MethodName ("getDatas" )
137+ Observable <List <ParcelableObj >> getDatas ();
138+
139+ }
81140}
0 commit comments