为了我们讲那个AIDL,我们今天先说一下,怎样在怎样调用服务里面的方法,其实调用服务里面的方法也很简单
就是使用bindService来启动一个服务,在绑定的时候拿到一个Binder对象
但是问题就来啦,拿到的Binder怎么知道我的Service里面有什么方法呢,所以我们就要写一个自己的Binder类啦
我们通过继承Binder类,然后再实现一个接口,接口里面定义Service里面的方法,然后,我们就可以通过拿到Binder来调用我们接口里面的方法啦
好,废话不多说,直接上代码,代码更容易理解
我们顺便复习一个startService和bindService的区别哈
先看一下界面
我们先把我们的接口写出来
com.xiaobin.service.IService
package com.xiaobin.service; public interface IService { public void invokeServiceMethod(); }
com.xiaobin.service.MyService
package com.xiaobin.service; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; public class MyService extends Service { @Override public IBinder onBind(Intent arg0) { System.out.println("onbind"); //返回我们自己写的,实现了IService接口的Binder return new MyBinder(); } @Override public void onCreate() { System.out.println("onCreate"); super.onCreate(); } @Override public void onDestroy() { System.out.println("onDestory"); super.onDestroy(); } @Override public void onStart(Intent intent, int startId) { System.out.println("onStart"); super.onStart(intent, startId); } @Override public boolean onUnbind(Intent intent) { System.out.println("onunbind"); return super.onUnbind(intent); } public void sayService() { System.out.println("调用service里面的方法"); } //实现一个接口,然后我们就在绑定服务的时候拿到这个Binder对象,然后调用接口里面的方法 class MyBinder extends Binder implements IService { @Override public void invokeServiceMethod() { //调用Service里面的方法 sayService(); } } }
com.xiaobin.service.MainActivity
package com.xiaobin.service; 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.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { private Button start = null; private Button stop = null; private Button bind = null; private Button unbind = null; private Button say = null; private Intent intent = null; private MyServiceConnection conn = null; private IService iService = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.main); start = (Button)this.findViewById(R.id.ser_start); stop = (Button)this.findViewById(R.id.ser_stop); bind = (Button)this.findViewById(R.id.ser_bind); unbind = (Button)this.findViewById(R.id.ser_unbind); say = (Button)this.findViewById(R.id.ser_say); start.setOnClickListener(this); stop.setOnClickListener(this); bind.setOnClickListener(this); unbind.setOnClickListener(this); say.setOnClickListener(this); intent = new Intent(this, MyService.class); conn = new MyServiceConnection(); } @Override public void onDestroy() { if(conn != null) { this.unbindService(conn); conn = null; } super.onDestroy(); } @Override public void onClick(View v) { switch(v.getId()) { case R.id.ser_start : this.startService(intent); break; case R.id.ser_stop : this.stopService(intent); break; case R.id.ser_bind : this.bindService(intent, conn, BIND_AUTO_CREATE); break; case R.id.ser_unbind : if(conn != null) { this.unbindService(conn); conn = null; } break; case R.id.ser_say : if(iService != null) { iService.invokeServiceMethod(); } else { Toast.makeText(this, "请先绑定服务", Toast.LENGTH_SHORT).show(); } break; } } class MyServiceConnection implements ServiceConnection { @Override public void onServiceConnected(ComponentName name, IBinder service) { //拿到在服务里面实现了IService接口的Binder对象 iService = (IService)service; } @Override public void onServiceDisconnected(ComponentName name) { } } }
好啦,到现在为止,我们的代码就写完的啦,现在我们要在AndroidManifest里面把Activity和Service注册一下
<activity android:name="com.xiaobin.service.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="com.xiaobin.service.MyService"></service>
这一次就到这里啦,有什么不明白的,可以问,我们下次讲的就是AIDL啦
今天源码下载