Messenger 实现Activity与Service通讯、进程通讯
信使 主要用于进程通讯详见SDK
android.os.Messenger
一、Activity通过Messenger发送信息给Service
1、创建项目Messenger 包名:com.Messenger.main
2、创建消息结构体MSG.java
package com.Messenger.main;
public class MSG {
static final int MSG_ONE = 1;
static final int MSG_TWO = 2;
static final int MSG_THREE = 3;
static final int MSG_FOUR = 4;
}
3、创建MessengerActivity.java
通过ServiceConnection服务通讯控制类接口来获得Service的连接并创建Messenger对象
重点:绑定服务器
bindService(new Intent(this,MessengerService.class),
this.mConnection, Context.BIND_AUTO_CREATE);
代码:import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MessengerActivity extends Activity {
private Messenger mServiceSend;
private boolean mBound;
private Button mButton;
/**
*服务通讯类接口
*/
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
mServiceSend = null;
mBound = false;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
/*
*Activity通过Service成功连接后返回的IBinder对象构建
*之后通过该IBunder与Service实现通讯
*/
mServiceSend = new Messenger(service);
mBound = true;
}
};
public void doService(){
/*
*创建并发送消息给Service
*/
try {
Message msg = Message.obtain(null, MSG.MSG_ONE,0,0);
mServiceSend.send(msg);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton = (Button)findViewById(R.id.button1);
mButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
doService();
}
});
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
//取消绑定
if(mBound){
unbindService(mConnection);
mBound = false;
}
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
//绑定到服务器
bindService(new Intent(this,MessengerService.class),
this.mConnection, Context.BIND_AUTO_CREATE);
}
}
4、让Messenger继承Service
package com.Messenger.main;
import android.app.Service;
import android.content.Intent;
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 MessengerService extends Service {
private final String log = "MessengerService";
DataStruct mDataInfo = new DataStruct();
/*
创建发送/接收消息的IncomingHandler
*/
final Messenger mMessenger = new Messenger(new IncomingHandler());
/**
* 来自客户端消息的handler
*/
class IncomingHandler extends Handler{
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what) {
case MSG.MSG_ONE:
Toast.makeText(getApplicationContext(), "Hello Activity.", Toast.LENGTH_LONG).show();
break;
case MSG.MSG_TWO:
break;
case MSG.MSG_THREE:
break;
case MSG.MSG_FOUR:
break;
default:
break;
}
}
}
/*
当绑定服务时,返回一个接口给messenger,为了发送消息给服务器
*/
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Log.i(log, "binding.");
return mMessenger.getBinder();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
Log.i(log, "Service Start.");
super.onStart(intent, startId);
}
主要实现:(main.xml见 Messenger 实现Activity与Service通讯、进程通讯(二) )Service通过返回Messenger的Binder给绑定它的Activity实现通讯。Service通过Handler监听返回信息,Activity通过ServiceConnection来获得Service的Binder并创建Activity的Messenger对象