activity与service交互有很多种,现在介绍一下messenger交互方法
messenger交互方法,类似于客户端访问服务器端,activity就是Client端,而service就是server端
MainActivity.java
package com.example.android_service_messager;
import android.annotation.SuppressLint;
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.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private boolean mBound;
private Button button1;
private Messenger messenger;
private Messenger responseClient = new Messenger(new IncomingHandler());
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mBound = false;
messenger = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mBound = true;
messenger = new Messenger(service);
}
};
@SuppressLint("HandlerLeak")
public class IncomingHandler extends Handler {
@SuppressLint("ShowToast")
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 2) {
Toast.makeText(getApplicationContext(), "唐雪蕾爱" + msg.obj, 1).show();
}
}
}
@Override
protected void onStart() {
super.onStart();
bindService(new Intent(this, MyService.class), conn, Context.BIND_AUTO_CREATE);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) this.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Message message = Message.obtain();
message.what = 1;
message.obj = "我爱你";
//最为关键,把message的
/**
* Optional Messenger where replies to this message can be sent. The
* semantics of exactly how this is used are up to the sender and
* receiver.
*/
message.replyTo = responseClient;
try {
messenger.send(message);
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
}
@Override
protected void onStop() {
super.onStop();
if (mBound) {
mBound = false;
messenger = null;
}
}
}
MyService.java
package com.example.android_service_messager;
import android.annotation.SuppressLint;
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.widget.Toast;
public class MyService extends Service {
public final static int update = 1;
private Messenger reply; //响应Client数据,从Service发送信息到Client端
@SuppressLint("HandlerLeak")
public class IncomingHandler extends Handler {
@SuppressLint("ShowToast")
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == update) {
//Toast.makeText(getApplicationContext(), "唐雪蕾" + msg.obj, 1).show();
Message message = new Message();
message.what = 2;
message.obj = "孙晨阳";
reply = msg.replyTo;
try {
reply.send(message);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//接收从Client发送的message
final Messenger messenger = new Messenger(new IncomingHandler());
@Override
public IBinder onBind(Intent intent) {
return messenger.getBinder();
}
}