Skip to content

Commit

Permalink
Messenger通信, 客户端实现
Browse files Browse the repository at this point in the history
  • Loading branch information
suzeyu1992 committed Sep 29, 2016
1 parent 8833697 commit 4b0f94b
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 2 deletions.
10 changes: 9 additions & 1 deletion IPC/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,26 @@

</activity>


<!--开始aidl通信-->
<activity android:name=".aidl.AidlClientActivity">

</activity>
<service
android:name=".aidl.AidlService"
android:enabled="true"
android:exported="true" />

<activity android:name=".aidl.AidlClientActivity">

<!--开始messenger通信-->
<activity android:name=".messenger.MessengerClientActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".messenger.MessengerService"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package com.szysky.note.ipc.messenger;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
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 com.szysky.note.ipc.R;

/**
* Author : suzeyu
* Time : 2016-09-29 上午10:07
* Blog : http://szysky.com
* GitHub : https://github.com/suzeyu1992
*
* ClassDescription : 使用Messenger通信的客户端
*/

public class MessengerClientActivity extends Activity implements ServiceConnection{

private static final String TAG = "sususu";

/**
* 服务端的 Messenger
*/
private Messenger mRemoteMessenger;

/**
* 本地构建的 Messenger
*/
private Handler mReplyHandler;
private Messenger mReplyMessenger;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.messenger_client);

HandlerThread handlerThread = new HandlerThread("MessengerClientActivity");
handlerThread.start();
mReplyHandler = new Handler(handlerThread.getLooper(), new ReplyHandlerCallback());

mReplyMessenger = new Messenger(mReplyHandler);
}

@Override
protected void onResume() {
super.onResume();
bindService(new Intent(getApplicationContext(), MessengerService.class), this, MessengerService.BIND_AUTO_CREATE);
}

@Override
protected void onPause() {
super.onPause();
unbindService(this);
}

@Override
protected void onDestroy() {
super.onDestroy();
mReplyHandler.getLooper().quit();

}

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// 利用服务端返回的 Messenger的IBinder 构建出一个Messenger
mRemoteMessenger = new Messenger(service);
}

@Override
public void onServiceDisconnected(ComponentName name) {
mRemoteMessenger = null;
}

/**
* 按钮发送
*/
public void onSendTextButton(View view){
String sendStr = "嘿嘿";

Message obtain = Message.obtain();
obtain.what = MessageAPI.SEND_TEXT_MSG;
obtain.obj = sendStr;
obtain.replyTo = mReplyMessenger;

try {
mRemoteMessenger.send(obtain);
} catch (RemoteException e) {
e.printStackTrace();
// 远程已被销毁
}
}

/**
* 接收服务端的回应
*/
private class ReplyHandlerCallback implements Handler.Callback {
@Override
public boolean handleMessage(Message msg) {
switch (msg.what){
case MessageAPI.MESSAGE_DELIVERED_MSG:

// 省略逻辑部分
Log.e(TAG, "客户端收到回应" );

break;
}
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

public class MessengerService extends Service{

private static final String TAG = Messenger.class.getSimpleName();
private static final String TAG = "sususu";
private Handler mMessageHandler;
private Messenger mMessenger;

Expand Down Expand Up @@ -63,10 +63,13 @@ public boolean handleMessage(Message msg) {
switch (msg.what){
case MessageAPI.SEND_TEXT_MSG:
delivered = sendTextMessage((String)msg.obj);
Log.d(TAG, "服务端收到 text消息");
break;

case MessageAPI.SEND_PHOTO_MSG:
delivered = sendPhotoMessage((Bitmap)msg.obj);
Log.d(TAG, "服务端收到 photo消息");

break;
}

Expand Down
12 changes: 12 additions & 0 deletions IPC/app/src/main/res/layout/messenger_client.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送消息"
android:onClick="onSendTextButton"/>

</LinearLayout>

0 comments on commit 4b0f94b

Please sign in to comment.