Created
February 12, 2014 03:13
-
-
Save showsky/8949424 to your computer and use it in GitHub Desktop.
Android Messenger sample (1. Server app 2. Client app)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!-- This is Client app --> | |
| <uses-permission android:name="com.miiitv.permission.SERVICE" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private Messenger mService; | |
| final Messenger mMessenger = new Messenger(new Handler() { | |
| @Override | |
| public void dispatchMessage(Message msg) { | |
| switch (msg.what) { | |
| case SERV_RES_HAHA: | |
| String s = msg.getData().getString("msg"); | |
| Bundle data = new Bundle(); | |
| data.putString("msg", "fuck"); | |
| sendMsg(CLNT_REQ_SURPRISE, data); | |
| break; | |
| } | |
| } | |
| }); | |
| private void sendMsg(int msgCode, Bundle data) { | |
| try { | |
| Message msg = Message.obtain(null, msgCode); | |
| msg.replyTo = mMessenger; | |
| if (data != null) { | |
| msg.setData(data); | |
| } | |
| mService.send(msg); | |
| } catch (RemoteException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| private ServiceConnection servConnection = new ServiceConnection() { | |
| @Override | |
| public void onServiceConnected(ComponentName name, IBinder service) { | |
| mService = new Messenger(service); | |
| } | |
| @Override | |
| public void onServiceDisconnected(ComponentName name) { | |
| mService = null; | |
| } | |
| }; | |
| protected void doBindService() { | |
| bindService(new Intent("com.miiitv.service.MESSENGER"), servConnection, BIND_AUTO_CREATE); | |
| isBound = true; | |
| } | |
| protected void doUnbindService() { | |
| if (isBound) { | |
| unbindService(servConnection); | |
| isBound = false; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Server extends Service { | |
| private static final String TAG = "Server"; | |
| public static final String ACTION = "com.miiitv.service.MESSENGER"; | |
| final Messenger mMessenger = new Messenger(new Handler() { | |
| @Override | |
| public void dispatchMessage(Message msg) { | |
| switch (msg.what) { | |
| case CLNT_REQ_HELLO: | |
| Bundle data = new Bundle(); | |
| data.putString("msg", "Hello world~"); | |
| respClnt(msg.replyTo, SERV_RES_HAHA, data); | |
| break; | |
| case CLNT_REQ_SURPRISE: | |
| String s = msg.getData().getString("msg"); | |
| break; | |
| } | |
| } | |
| private void respClnt(Messenger clnt, int msgCode, Bundle data) { | |
| try { | |
| Message msg = Message.obtain(null, msgCode); | |
| msg.replyTo = mMessenger; | |
| if (data != null) | |
| msg.setData(data); | |
| clnt.send(msg); | |
| } catch (RemoteException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| }); | |
| @Override | |
| public IBinder onBind(Intent intent) { | |
| Log.e(TAG, "onBind"); | |
| return mMessenger.getBinder(); | |
| } | |
| // ...... | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!-- This is Server app --> | |
| <permission | |
| android:name="com.miiitv.permission.SERVICE" | |
| android:protectionLevel="normal" > | |
| </permission> | |
| <uses-permission android:name="com.miiitv.permission.SERVICE" /> | |
| <service | |
| android:name="com.miiitv.messenger.Server" | |
| android:exported="true" | |
| android:permission="com.miiitv.permission.SERVICE" | |
| android:process=":remote" > | |
| <intent-filter> | |
| <action android:name="com.miiitv.service.MESSENGER" /> | |
| </intent-filter> | |
| </service> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
這是 Android 跨 process 溝通方式使用 Messenger (信使) 代表這是一個非同步的方式,不需要考慮 concurrent 問題,和 AIDL溝通方式一樣屬於誇 process 溝通,但是此方式需要考慮 concurrent 問題。