标签:
<service android:name=".StudentService"> <intent-filter > <action android:name="com.njulya.REMOTESERVICE"/> </intent-filter> </service>
import com.njulya.aidl.StudentQuery; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; public class StudentService extends Service { IBinder iBinder; String[] names = {"张阳","李永安","潘杰"}; @Override public IBinder onBind(Intent intent) { iBinder = new StudentBinder(); return iBinder; } /** * 实现IBinder接口,用于通信 * @author Andy */ private class StudentBinder extends StudentQuery.Stub{ @Override //aidl文件中描述的接口 public String query(int no) throws RemoteException { return names[no-1];//业务代码 } } }
package com.njulya.remoteserviceclient; import com.njulya.aidl.StudentQuery; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { private EditText studentId; private TextView content; private StudentQuery studentQuery; private ServiceConnection conn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); studentId = (EditText)this.findViewById(R.id.student_id); content = (TextView)this.findViewById(R.id.content); conn = new StudentConnetion(); Intent service = new Intent("com.njulya.REMOTESERVICE"); this.bindService(service, conn, BIND_AUTO_CREATE); } /** * 按键触发业务代码 * @param v */ public void queryOnClick(View v){ int no = Integer.valueOf(studentId.getText().toString()); try { String studentName = studentQuery.query(no); content.setText(studentName); } catch (RemoteException e) { e.printStackTrace(); } } private class StudentConnetion implements ServiceConnection{ @Override public void onServiceConnected(ComponentName name, IBinder service) { //将IBinder代理对象转换为接口类型 studentQuery = StudentQuery.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { studentQuery = null; } } @Override protected void onDestroy() { unbindService(conn); super.onDestroy(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
package com.njulya.intsum; import android.os.Bundle; import android.os.IBinder; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { private EditText num1; private EditText num2; private TextView sum; private ServiceConnection conn; //自定义的公共接口,用于求两数之和 private SumOfTwo sumOfTwo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); num1 = (EditText)this.findViewById(R.id.num1); num2 = (EditText)this.findViewById(R.id.num2); sum = (TextView)this.findViewById(R.id.sum); Button button = (Button)this.findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { sum(); } }); conn = new SumConnetion(); Intent service = new Intent(this, SumService.class); this.bindService(service, conn, BIND_AUTO_CREATE); } protected void sum() { int number1 = Integer.valueOf(num1.getText().toString()); int number2 = Integer.valueOf(num2.getText().toString()); int result = sumOfTwo.sum(number1, number2); sum.setText(String.valueOf(result)); } private class SumConnetion implements ServiceConnection{ @Override public void onServiceConnected(ComponentName name, IBinder service) { sumOfTwo = (SumOfTwo)service; } @Override public void onServiceDisconnected(ComponentName name) { sumOfTwo = null; } } @Override protected void onDestroy() { unbindService(conn); super.onDestroy(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
package com.njulya.intsum; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; public class SumService extends Service { @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return new SumBinder(); } public class SumBinder extends Binder implements SumOfTwo{ @Override public int sum(int number1, int number2) { return number1 + number2; } } }
标签:
原文地址:http://www.cnblogs.com/lya-nju/p/4236501.html