客户端和服务端显示如图:
日志里:
清单文件中注册服务,添加动作
服务端APP:
1.创建一个继承service的类,
2.创建一个内部类继承Handler以处理客户端传过来的消息,handleMessage方法里处理客户端传的数据
3.构建Messenger对象,以处理handler消息对象
Messenger mes=new Messenger(new MyHandler());
4.onBind方法里返回的是:mes.getBinder()
服务类:
<span style="font-size:18px;">package com.example.day23_messageservice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
public class MyService extends Service{
@Override
public IBinder onBind(Intent intent) {
return mes.getBinder();
}
//构建Messenger对象,以处理handler消息对象
Messenger mes=new Messenger(new MyHandler());
//处理客户端传过来的消息
class MyHandler extends Handler
{
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch(msg.what)
{
case 1:
System.out.println("客户端传来的消息"+msg.arg1);
break;
}
}
}
}
</span>
客户端APP:
1.声明信使对象: Messenger message;
2.按钮事件里传送数据: Message msg=Message.obtain();
msg.arg1=200;
msg.what=1;
message.send(msg);
3.构建一个服务连接对象:ServiceConnection sc=new ServiceConnection() {。。。。。。}
4.步骤3中。。。。里的内容,onServiceConnected方法中写:message=new Messenger(service);
onServiceDisconnected方法里写:message=null;
5.onCreate或onStart方法里创建Intent,绑定服务
逻辑代码:
<span style="font-size:18px;">package com.example.day23_messageclient;
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;
public class MainActivity extends Activity {
Messenger mes;//声明信使Messenger对象
boolean isBound;//判断是否绑定
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
Intent intent=new Intent();
intent.setAction("ww.ss.xx");
bindService(intent, sc, Context.BIND_AUTO_CREATE);
}
public void click(View v)
{
try
{
Message msg=Message.obtain();
msg.arg1=200;
msg.what=1;
mes.send(msg);
} catch (RemoteException e) {
e.printStackTrace();
}
}
ServiceConnection sc=new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mes=null;
isBound=false;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//远程 服务连接成功后 构建信使对象 服务端的信使对象传给服务端的信使对象
mes=new Messenger(service);
isBound=true;
}
};
}
</span>
布局文件:
<span style="font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送消息"
android:onClick="click" />
</RelativeLayout>
</span>
完善版:带服务端向客户端回传消息的:
如图:
步骤同上面的类似
服务端APP:
清单文件里注册服务。。。
service类文件:
<span style="font-size:18px;">package com.example.day23_messageservice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
public class MyService extends Service{
//客户端的信使对象 回传消息
Messenger rely;
@Override
public IBinder onBind(Intent intent) {
return mes.getBinder();
}
//构建Messenger对象,以处理handler消息对象
Messenger mes=new Messenger(new MyHandler());
//处理客户端传过来的消息
class MyHandler extends Handler
{
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch(msg.what)
{
case 1:
System.out.println("客户端传来的消息"+msg.arg1);
//回传消息
try
{
Message msg2=Message.obtain();
msg2.arg2=10;
rely=msg.replyTo;
rely.send(msg2);
}
catch (RemoteException e)
{
e.printStackTrace();
}
break;
}
}
}
}
</span>
客户端APP:
逻辑代码:
<span style="font-size:18px;">package com.example.day23_messageclient;
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.View;
import android.widget.Toast;
public class MainActivity extends Activity {
Messenger mes;//声明信使Messenger对象
boolean isBound;//判断是否绑定
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
Intent intent=new Intent();
intent.setAction("ww.ss.xx");
bindService(intent, sc, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
if(isBound)
{
unbindService(sc);//解除绑定
}
}
public void click(View v)
{
try
{
Message msg=Message.obtain();
msg.arg1=200;
msg.what=1;
//客户端的信使对象传递到服务端,接收服务端回传的数据
msg.replyTo=rely;
mes.send(msg);
} catch (RemoteException e) {
e.printStackTrace();
}
}
ServiceConnection sc=new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mes=null;
isBound=false;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//远程 服务连接成功后 构建信使对象 服务端的信使对象传给服务端的信使对象
mes=new Messenger(service);
isBound=true;
}
};
//处理服务端回传的数据
Messenger rely=new Messenger(new Handler(){
public void handleMessage(Message msg) {
int str=msg.arg2;
Toast.makeText(getApplicationContext(), "服务端回传的消息是:"+str, 0).show();
};
});
}
</span>
布局文件同第一种一样就不写了