随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)、QQ技术交流群(183198395)。
在上文涉及了用startService和stopService方法启动和关闭Service,但是访问者和Service并不存在太多的关联。所以如果要让访问者和Service可以进行方法调用和数据交换,就需要使用bindService和unbindService方法启动和关闭Service。Context的bindService()方法的完整方法签名为:bindService(Intent service,ServiceConnection conn,int flags),该方法三个参数解释如下:
注意上面onServiceConnected方法中有个IBinder对象,该对象即可实现被绑定Service之间的通信。
下面通过一个简单实例来演示,该Activity只是访问Service的一个简单的Count值。实际上可以让MyBinder去操作Service中更多的数据,这完全取决于实际业务的需要。
Activity:
package com.lovo.testservice;
import android.app.Activity;
import android.app.Service;
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.widget.Toast;
import com.lovo.service.BindService;
public class MainActivity extends Activity {
private BindService.MyBinder binder;
private Intent intent;
private boolean isBinded;
// 定义一个ServiceConnection对象
private ServiceConnection conn = new ServiceConnection() {
// 当该Activity与Service连接成功时回调该方法
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
System.out.println("--Service Connected");
// 获取Service的onBind方法返回的MyBinder对象
binder = (BindService.MyBinder) service;
}
// 当该Activity与Service断开连接时回调该方法
@Override
public void onServiceDisconnected(ComponentName name) {
System.out.println("--Service Disconnected");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent();
// 为Intent设置Action属性
intent.setAction("com.lovo.myservice.BIND_SERVICE");
}
public void click(View v) {
switch (v.getId()) {
case R.id.main_btn_bind:
isBinded = true;
// 绑定指定Service
bindService(intent, conn, Service.BIND_AUTO_CREATE);
break;
case R.id.main_btn_unbind:
if (isBinded) {
// 解除绑定Service
unbindService(conn);
isBinded = false;
}
break;
case R.id.main_btn_getStatus:
// 获取并显示Service的count值
Toast.makeText(this, "Service的count值为:" + binder.getCount(),
Toast.LENGTH_SHORT).show();
}
}
}
为了方便阅读,本文中ServiceConnection接口是在Activity内部实现的,以及下面Service中的MyBinder类也是在Service内部定义的。在实际开发中,可以定义外部类MyServiceConnection实现ServiceConnection接口和定义一个外部类MyBinder继承Binder。
Service:
package com.lovo.service;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class BindService extends Service {
private int count;
private boolean quit;
// 定义onBind方法所返回的对象
private MyBinder binder = new MyBinder();
// 通过继承Binder来实现IBinder类
public class MyBinder extends Binder {
public int getCount() {
// 获取Service的运行状态:count
return count;
}
}
// 继承Service必须实现的方法
@Override
public IBinder onBind(Intent intent) {
// 返回IBinder对象
return binder;
}
// Service被创建时回调该方法
@Override
public void onCreate() {
super.onCreate();
System.out.println("Service is Created");
// 启动一条线程,动态地修改count状态值
new Thread() {
public void run() {
while (!quit) {
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
count++;
}
}
}.start();
}
// Service被断开连接时回调该方法
@Override
public boolean onUnbind(Intent intent) {
System.out.println("Service is Unbinded");
return true;
}
// Service被关闭之前回调该方法
@Override
public void onDestroy() {
super.onDestroy();
this.quit = true;
System.out.println("Service is Destroyed");
}
}
布局XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/main_btn_bind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="绑定Service" />
<Button
android:id="@+id/main_btn_unbind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="解除绑定" />
<Button
android:id="@+id/main_btn_getStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="获得Service数据" />
</LinearLayout>