Messenger 使用

3.1 客户端步骤:

3.1.1 创建一个handler对象,并实现 handleMessage() 方法,用户接收客户端的消息。

3.1.2 创建Messenger ,封装handler。

3.1.3 用Messenger的 getBinder() 方法获取一个IBinder对象,通过 onBind 返回给客户端。

服务端的Service:

package cn.zzw.messenger.server;

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 static final String TAG = "MessengerService";
private MessengerHandler mHandler = new MessengerHandler();
private Messenger mMessenger = new Messenger(mHandler);
private static final int MSG_SUM = 10086;

private class MessengerHandler extends Handler {
@Override
public void handleMessage(Message msg) {
if(msg.what==MSG_SUM)
{
Log.e(TAG,"handleMessage");
try {
int arg1 = msg.arg1;
int arg2=msg.arg2;
Message msgToClient=Message.obtain(msg);
msgToClient.arg1=arg1+arg2;
msg.replyTo.send(msgToClient);
} catch (RemoteException e) {

}
}

}
}

@Override
public IBinder onBind(Intent intent) {
return mMessenger.getBinder();
}

@Override
public void onDestroy() {
super.onDestroy();
mHandler.removeCallbacksAndMessages(null);
}
}
3.2 客户端步骤:

3.2.1 在 Activity 中绑定服务。

3.2.2 创建ServiceConnection并在其中使用 IBinder 将 Messenger实例化

3.2.3 使用Messenger向服务端发送消息

客户端主要代码:

package cn.zzw.messenger.client;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
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.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
private static final String TAG = "MessengerClient";
private TextView mTv;
private Messenger mService;
private static final int MSG_SUM = 10086;
private MessengerHandler mHandler = new MessengerHandler();
private Messenger mMessenger = new Messenger(mHandler);

private class MessengerHandler extends Handler {
@Override
public void handleMessage(Message msg) {

if(msg.what==MSG_SUM)
{
Log.e(TAG,"handleMessage");
int arg1 = msg.arg1;
Log.e(TAG,"handleMessage:"+arg1);
Toast.makeText(MainActivity.this,"The Result is "+arg1,Toast.LENGTH_SHORT).show();
}

}
}
private ServiceConnection conn=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
//获取服务端关联的Messenger对象
mService = new Messenger(iBinder);
Log.e(TAG,"onServiceConnected");
}

@Override
public void onServiceDisconnected(ComponentName componentName) {
Log.e(TAG,"onServiceDisconnected");
mService=null;
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindToServserService();
mTv = findViewById(R.id.mTv);
findViewById(R.id.mBtn_cal).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Message msg = Message.obtain(null, MSG_SUM, 10, 500);
msg.replyTo = mMessenger;
if(null!=mService)
{
//往服务端发送消息
try {
Log.e(TAG,"mService.send");
mService.send(msg);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
});
}

/*
* 绑定服务端的Service
*/
private void bindToServserService() {
Intent intent=new Intent();
intent.setPackage("cn.zzw.messenger.server");
intent.setAction("CN.ZZW.MESSENGER.SERVER");
bindService(intent,conn,BIND_AUTO_CREATE);
}

@Override
protected void onDestroy() {
super.onDestroy();
unbindService(conn);
mHandler.removeCallbacksAndMessages(null);
}

 

posted @ 2019-09-12 17:24  水至清明  阅读(532)  评论(0编辑  收藏  举报