标签:this str div _id tom remote string ibinder prot
Service端
<service android:name="com.atguigu.l07_service.remote.MyRemoteService"> <intent-filter> <action android:name="com.atguigu.l07_service.remote.MyRemoteService.Action"/> </intent-filter> </service>
public class MyRemoteService extends Service { @Override public IBinder onBind(Intent intent) { Log.e("TAG", "onBind()"); return new StudentService(); } @Override public boolean onUnbind(Intent intent) { Log.e("TAG", "onUnbind()"); return super.onUnbind(intent); } //处理Student相关的业务逻辑类 class StudentService extends IStudentService.Stub { @Override public Student getStudentById(int id) throws RemoteException { Log.e("TAG", "Service getStudentById() "+id); return new Student(id, "Tom", 10000); } } }
client
public class MainActivity extends Activity { private EditText et_aidl_id; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_aidl_id = (EditText) findViewById(R.id.et_aidl_id); } private ServiceConnection conn; private IStudentService studentService; public void bindRemoteService(View v) { Intent intent = new Intent( "com.atguigu.l07_service.remote.MyRemoteService.Action"); if (conn == null) { conn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { } @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.e("TAG", "onServiceConnected()"); studentService = IStudentService.Stub.asInterface(service); } }; bindService(intent, conn, Context.BIND_AUTO_CREATE); Toast.makeText(this, "绑定Service", 0).show(); } else { Toast.makeText(this, "已经绑定Service", 0).show(); } } public void invokeRemote(View v) throws RemoteException { if(studentService!=null) { int id = Integer.parseInt(et_aidl_id.getText().toString()); Student student = studentService.getStudentById(id); Toast.makeText(this, student.toString(), 0).show(); } } public void unbindRemoteService(View v) { if (conn != null) { unbindService(conn); conn = null; studentService = null; Toast.makeText(this, "解绑Service", 0).show(); } else { Toast.makeText(this, "还未绑定Service", 0).show(); } } }
标签:this str div _id tom remote string ibinder prot
原文地址:https://www.cnblogs.com/znsongshu/p/9355893.html