|
| 1 | +package com.example.andlinker; |
| 2 | + |
| 3 | +import android.graphics.Rect; |
| 4 | +import android.support.v7.app.AppCompatActivity; |
| 5 | +import android.os.Bundle; |
| 6 | +import android.view.View; |
| 7 | +import android.widget.Toast; |
| 8 | + |
| 9 | +import com.codezjx.andlinker.AndLinker; |
| 10 | + |
| 11 | +import butterknife.ButterKnife; |
| 12 | +import butterknife.OnClick; |
| 13 | + |
| 14 | +public class BindingActivity extends AppCompatActivity { |
| 15 | + |
| 16 | + private static final String REMOTE_SERVICE_PKG = "com.example.andlinker"; |
| 17 | + public static final String REMOTE_SERVICE_ACTION = "com.example.andlinker.REMOTE_SERVICE_ACTION"; |
| 18 | + |
| 19 | + private AndLinker mLinker; |
| 20 | + private IRemoteService mRemoteService; |
| 21 | + |
| 22 | + @Override |
| 23 | + protected void onCreate(Bundle savedInstanceState) { |
| 24 | + super.onCreate(savedInstanceState); |
| 25 | + setContentView(R.layout.activity_binding); |
| 26 | + ButterKnife.bind(this); |
| 27 | + |
| 28 | + mLinker = new AndLinker.Builder(this) |
| 29 | + .packageName(REMOTE_SERVICE_PKG) |
| 30 | + .action(REMOTE_SERVICE_ACTION) |
| 31 | + .build(); |
| 32 | + mLinker.bind(); |
| 33 | + mLinker.registerObject(mRemoteCallback); |
| 34 | + |
| 35 | + mRemoteService = mLinker.create(IRemoteService.class); |
| 36 | + } |
| 37 | + |
| 38 | + @OnClick({R.id.btn_pid, R.id.btn_basic_types, R.id.btn_callback, R.id.btn_directional, R.id.btn_oneway}) |
| 39 | + public void onClick(View view) { |
| 40 | + switch (view.getId()) { |
| 41 | + case R.id.btn_pid: |
| 42 | + Toast.makeText(this, "Server pid: " + mRemoteService.getPid(), Toast.LENGTH_SHORT).show(); |
| 43 | + mRemoteService.getPid(); |
| 44 | + break; |
| 45 | + case R.id.btn_basic_types: |
| 46 | + mRemoteService.basicTypes(1, 2L, true, 3.0f, 4.0d, "str"); |
| 47 | + break; |
| 48 | + case R.id.btn_callback: |
| 49 | + mRemoteService.registerCallback(mRemoteCallback); |
| 50 | + break; |
| 51 | + case R.id.btn_directional: |
| 52 | + int[] arr = {1, 2, 3}; |
| 53 | + ParcelableObj parcelableObj = new ParcelableObj(); |
| 54 | + Rect rect = new Rect(10, 20, 30, 40); |
| 55 | + mRemoteService.directionalParamMethod(arr, parcelableObj, rect); |
| 56 | + Toast.makeText(this, "After directionalParamMethod parcelableObj: " + parcelableObj |
| 57 | + + " rect: " + rect, Toast.LENGTH_LONG).show(); |
| 58 | + break; |
| 59 | + case R.id.btn_oneway: |
| 60 | + mRemoteService.onewayMethod("oneway"); |
| 61 | + Toast.makeText(this, "After oneway method client.", Toast.LENGTH_SHORT).show(); |
| 62 | + break; |
| 63 | + default: |
| 64 | + break; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + protected void onDestroy() { |
| 70 | + super.onDestroy(); |
| 71 | + mLinker.unbind(); |
| 72 | + } |
| 73 | + |
| 74 | + private final IRemoteCallback mRemoteCallback = new IRemoteCallback() { |
| 75 | + @Override |
| 76 | + public void onValueChange(int value) { |
| 77 | + // Invoke when server side callback |
| 78 | + Toast.makeText(BindingActivity.this, "Server callback value: " + value, Toast.LENGTH_SHORT).show(); |
| 79 | + } |
| 80 | + }; |
| 81 | +} |
0 commit comments