android开发--activity与service交互之messenger

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();
	}

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值