转载请注明来自:http://blog.youkuaiyun.com/ly20116/article/details/51045180
一、使用Intent进行通信
主要是使用Intent传递简单数据,且是在启动Service时传递,启动后就不通信,可以用来设置一下Service的配置。
Intent intent=new Intent(MainActivity.this,MyService.class);
intent.putExtra("data","aaaaa");
startService(intent);
在Service中的onStartCommand()的方法中获取Intent,从而获取到数据,实现Activity与Service间通信。
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
String data=intent.getStringExtra("data");
Log.i("TAG", data);
return super.onStartCommand(intent, flags, startId);
}
二、绑定Service通信
主要实现Activity中向Service传递数据,实时改变Service的状态
1、在Service中实现类MyBinder类继承至Binder,实现一个方法setData(),用于修改数据。
public class MyBinder extends Binder{
public void setData(String data){
MyService.this.data=data;
}
}
2、在onBind()方法中返回MyBinder对象
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return new MyBinder();
}
3、在Activity中实现ServiceConnection接口,同时实现onServiceConnected()和onServiceDisconnected()方法,当然你要可以自定义一个ServiceConnection对象并实现它的2个方法。
获取onBind()方法返回的IBind对象。
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
myBinder=(MyBinder)service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
/** 定义服务绑定时的回调方法,用于传给bindService() */
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
myBinder=(MyBinder)service;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
4、绑定Service,并与之通信
Intent bindIntent=new Intent(this,MyService.class);
bindService(bindIntent, this, Context.BIND_AUTO_CREATE);
bindService()函数详解
@Override
public boolean bindService(Intent service, ServiceConnection conn,
int flags) {
return mBase.bindService(service, conn, flags);
}
参数分别为:
1、Intent对象
2、ServiceConnectino对象
3、指明绑定选项的标志,通常应该是BIND_AUTO_CREATE,表示如果服务未启动的话则创建服务。其它可能的值包括BIND_DEBUG_UNBIND和BIND_NOT_FOREGROUND,或者为0表示不指定
5、解除绑定
unbindService(this);
三、绑定Service,监听Service的内部状态
监听Service的状态,Service的数据返回给Activity
1、定义一个接口CallBack
public interface CallBack {
void DataChange(String data);
}
2、在Service中创建CallBack对象,并实现get和set方法。
private CallBack callback=null;
public CallBack getCallback() {
return callback;
}
public void setCallback(CallBack callback) {
this.callback = callback;
}
3、在MyBinder类中新建一个方法,返回Service对象
public class MyBinder extends Binder{
public void setData(String data){
MyService.this.data=data;
}
//获取当前Service对象
public MyService getService(){
return MyService.this;
}
}
4、在线程中调用CallBack的方法
String str=i+":"+data;
Log.i("TAG", str);
if(callback!=null){
callback.DataChange(str);
}
5、在Activity的onServiceConnection方法中,获取Service对象,并设置CallBack:
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
myBinder=(MyBinder)service;
myBinder.getService().setCallback(new CallBack() {
@Override
public void DataChange(String data) {
// TODO Auto-generated method stub
Message msg=new Message();
msg.obj=data;
handler.sendMessage(msg);
}
});
}
由于是在子线程中,要改变UI界面,需要在主线程,所以使用Handler+Message实现
Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
outText.setText(msg.obj.toString());
};
};
Service通信实例下载链接:http://download.youkuaiyun.com/detail/ly20116/9489201