Android学习笔记之service通信(Messenger)
Android学习笔记之service通信(Messenger)
1、activity通过bindService方法建立与service的连接,然后使用sMessenger向ServerObserverService发送message
2、ServerObserverService继承 service
private MyHandler cMeeeengerHandler = new MyHandler();
private Messenger sMessenger = new Messenger( cMeeeengerHandler);)
3、重写service里的onBind方法,service里的sMessenger得到IBinder。在与之绑定的activity的onServiceConnected方法里使用这个IBinder来实例化activity里的sMessenger。使用此sMessenger向service发送message。(message通过replyto指定了回应的Messenger 即cMessenger)
4、这时serive的sMessenger即有了message信息,又sMessenger是通过重写了handleMessage的Handler来建立的。故handleMessage可以对message进行处理。
5、service通过handler里的handleMessage处理得到的message,
(这个message告诉了回应的Messenger赋值给cMessenger)
产生回应的Message m,通过cMessenger发送给activity。
//service和activity里都是通过handlerMessage来对收到的消息进行处理,以上activity也可以理解为client
//这个逻辑是真的搞人,我也不知道我这样理解的对不对,目前感觉没问题,欢迎指正
Service
package es.source.code.service;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
public class ServerObserverService extends Service {
private static final String Tag = "ServerObserverService";
public Messenger cMessenger;
private MyHandler cMeeeengerHandler = new MyHandler();
public Messenger sMessenger = new Messenger( cMeeeengerHandler);
private class MyHandler extends Handler{
@Override
public void handleMessage(Message msg){
super.handleMessage(msg);
switch(msg.what){
case 1:
//开始更新
System.out.println("Msg_Food_Get_Start");
System.out.println("service:获取客户端message 1 成功");
try {
for(int i = 3; i > 0; i--) {
Thread.sleep(300);
Message m = getFoodInfo();
System.out.println(m.what);
System.out.println(m.getData().get("name"));
System.out.println(m.getData().get("storage"));
cMessenger = msg.replyTo;
try {
cMessenger.send(m);
System.out.println("发送菜品信息成功");
} catch (RemoteException e) {
e.printStackTrace();
}
}
}catch(InterruptedException e){
e.printStackTrace();
}
break;
case 0:
Thread.interrupted();
System.out.println("Msg_Food_Get_Stop");
System.out.println("service:获取客户端message 0 成功");
//停止更新
break;
default:
}
}
}
@Override
public IBinder onBind(Intent intent){
Log.w("TAG", "Service is binded");
return sMessenger.getBinder();
}
public Message getFoodInfo(){
Message m = Message.obtain();
Bundle bundle = new Bundle();
bundle.putString("name", "new food");
int storage = (int)(Math.random()*20);
bundle.putInt("storage", storage);
m.setData(bundle);
m.what = 10;
return m;
}
acticity
Messenger cMessenger = new Messenger(new MyHandler());
Messenger sMessenger;
private class MyHandler extends Handler{
@Override
//对得到的消息进行处理
public void handleMessage(final Message msg){
if(msg.what == 10){
System.out.println("fv:收到服务器发来的消息");
String name = (String)msg.getData().get("name");
int storage = (int)msg.getData().get("storage");
Intent toUpdate = new Intent(FoodView.this, UpdateService.class);
toUpdate.putExtra("new food", msg.getData());
startService(toUpdate);
}
}
};
private ServiceConnection mConnection = new ServiceConnection() {
@Override
//这里就得到service得到的IBinder。所以通过它得到sMessenger,可以通过此sMessenger
//向service发送消息
//因为下面有指出message的replyto为cMessenger,而cMessenger通过handler创建,所以可用handler的方法定义对service传来的消息的处理方法
public void onServiceConnected(ComponentName name, IBinder service) {
sMessenger = new Messenger(service);
System.out.println("fv:connected");
}
@Override
public void onServiceDisconnected(ComponentName name) {
sMessenger = null;
System.out.println("fv:disconnected");
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.food_view_2);
Intent bs = new Intent(this, ServerObserverService.class);
bindService(bs, mConnection, Context.BIND_AUTO_CREATE);
}
//处理消息
case R.id.action_service_start:
String transto = "默认消息";
Message message = Message.obtain();
if(item.getTitle().toString().equals("启动实时更新")){
item.setTitle("停止实时更新");
message.what = 1;
Bundle bundle = new Bundle();
bundle.putString("toService",transto);
message.setData(bundle);
message.replyTo = cMessenger;
try{
sMessenger.send(message);
System.out.println("fv:发送消息ed!");
}catch(RemoteException e){
e.printStackTrace();
}
System.out.println("启动");
}else{
item.setTitle("启动实时更新");
System.out.println("停止");
message.what = 0;
Bundle bundle = new Bundle();
bundle.putString("toService",transto);
message.setData(bundle);
message.replyTo = cMessenger;
try{
sMessenger.send(message);
System.out.println("fv:发送消息ed!");
}catch(RemoteException e){
e.printStackTrace();
}
}
break;
Manifest中需要注册servicce
<service android:name="es.source.code.service.ServerObserverService"></service>