Service想要与Activity进行数据交互,首先Activity先得绑定Service.bound service是service 的实现,它允许其他应用程序绑定到它并与之交互。要提供bound service,我们必须实现onBind()回调方法。这个方法返回一个内部对象定义的编程接口,Activity可以使用与Service进行交互。那么具体该如何实现呢,首先我们还是一样先创建一个MyService继承Service。然后如何设置:呢。(1)在你的Service,创建一个Binder实例,返回当前的Service实例以及一个Activity可以调用公共方法。(2)返回这个实例的Binder在onBind()回调方法。(3)在Activity里创建ServiceConnection,在onServiceConnected()回调方法接受Binder,并得到服务器的实例。讲的比较啰嗦哦,我们还是看代码来细细品味吧。
Service的代码
package com.example.f22_service02; import java.util.Random; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.os.Parcel; import android.os.RemoteException; import android.util.Log; public class HelloService extends Service { private MyBinder binder=new MyBinder(); public class MyBinder extends Binder{ public HelloService getService(){ return HelloService.this; //返回Service实例,使主程序能调用该方法 } @Override protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException { // TODO Auto-generated method stub Log.i("TAG", "------>"+data.readInt()); Log.i("TAG", "------>"+data.readString()); reply.writeInt(2); reply.writeString("Rose"); return super.onTransact(code, data, reply, flags); } } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return binder; //回掉方法 } public int getRandom(){ return new Random().nextInt(100); } @Override public void onCreate() { // TODO Auto-generated method stub Log.i("TAG", "------>Create"); super.onCreate(); } @Override public boolean onUnbind(Intent intent) { // TODO Auto-generated method stub Log.i("TAG", "------>Unbind"); return super.onUnbind(intent); } }
Activity的方法
package com.example.f22_service02; import com.example.f22_service02.HelloService.MyBinder; import android.os.Bundle; import android.os.IBinder; import android.os.Parcel; import android.os.RemoteException; import android.annotation.SuppressLint; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; @SuppressLint("Recycle") public class MainActivity extends Activity implements OnClickListener { private Button button, button2, button3; private MyBinder binder; private HelloService helloService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) this.findViewById(R.id.button1); button2 = (Button) this.findViewById(R.id.button2); button3 = (Button) this.findViewById(R.id.button3); button.setOnClickListener(this); button2.setOnClickListener(this); button3.setOnClickListener(this); } private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub Log.i("ACTIVITY", "---------〉失去绑定"); } @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub binder = (MyBinder) service; // 绑定Service,并获得Service实例,接下来就可以调用Service中的方法了 helloService = binder.getService(); Log.i("ACTIVITY", "---------〉绑定成功"); } }; @SuppressLint("Recycle") @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.button1: //绑定Service Intent intent=new Intent(MainActivity.this,HelloService.class); bindService(intent, connection, Context.BIND_AUTO_CREATE);//第三个参数是一个标志显示选项绑定 break; case R.id.button2: //实现Activity与Service的数据交互 //Parcel是一个容器,能包含消息(数据和对象引用),可以通过一个IBinde发送 Parcel data=Parcel.obtain(); data.writeInt(helloService.getRandom()); data.writeString("jack"); Parcel reply=Parcel.obtain(); try { binder.transact(IBinder.LAST_CALL_TRANSACTION, data, reply, 0); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } Log.i("ACTIVITY", "----Service回传的值---->"+reply.readInt()); Log.i("ACTIVITY", "----Service回传的值---->"+reply.readString()); break; case R.id.button3: //解除绑定 unbindService(connection); break; } } }