日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android跨进程通信一 Messenger

發布時間:2025/3/21 Android 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android跨进程通信一 Messenger 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

實現客戶端與服務端之間的交互

說明:
????????Messenger是信使的意思,從它的名字就可以了解到它充當著信差的角色。Android通過它實現跨進程通信,主要有客戶端信使與服務端信使兩種角色。

????????當客戶端調用bindService( )的時候,服務端會通過onBind( )方法將Ibinder傳遞給客戶端,然后客戶端通過ServiceConnection中的方法從Ibinder中獲得服務端的信使,客戶端可以通過服務端的信使把消息傳遞給服務端,最重要的一點是,客戶端可以講自己的信使即客戶端信使放入消息中一起傳遞到服務端中,這樣不僅可以從客戶端傳消息給服務端,還可以從服務端中返回信息給客戶端。


服務端Server.apk

配置文件

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.android.server" ><application android:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/AppTheme"><activity android:name=".MainActivity" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><service android:name=".MessengerService"android:enabled="true"android:exported="true"><intent-filter><action android:name="android.intent.action.SERVICE"/><category android:name="android.intent.category.DEFAULT"/></intent-filter></service></application></manifest>

布局

<?xml version="1.0" encoding="utf-8"?> <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:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:paddingBottom="@dimen/activity_vertical_margin"tools:context=".MainActivity"><TextView android:text="服務端"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="100sp"android:layout_centerInParent="true"/></RelativeLayout>

服務

package com.android.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;/*** 服務端*/ public class MessengerService extends Service {public static final int MSG_SUM = 1;public static final int MSG_RET = 2;public Messenger clientMessenger;public Messenger serverMessenger = new Messenger(new Handler(){@Overridepublic void handleMessage(Message msg) {switch (msg.what){case MSG_SUM:clientMessenger = msg.replyTo;Message message = Message.obtain(null,MSG_RET);message.arg1 = msg.arg1+msg.arg2;try {clientMessenger.send(message);} catch (RemoteException e) {e.printStackTrace();}break;default:handleMessage(msg);}}});@Overridepublic IBinder onBind(Intent intent) {return serverMessenger.getBinder();}@Overridepublic boolean onUnbind(Intent intent) {return true;}@Overridepublic void onRebind(Intent intent) {}}

界面

package com.android.server;import android.support.v7.app.AppCompatActivity; import android.os.Bundle;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);} }

客戶端apk

配置

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.android.client" ><application android:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/AppTheme" ><activity android:name=".MainActivity" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

布局

<?xml version="1.0" encoding="utf-8"?> <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:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:paddingBottom="@dimen/activity_vertical_margin"tools:context=".MainActivity"><EditText android:id="@+id/num1"android:layout_width="100dp"android:layout_height="wrap_content" /><TextView android:id="@+id/plus"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="+"android:layout_toRightOf="@id/num1"android:textSize="20sp"/><EditText android:id="@+id/num2"android:layout_width="100dp"android:layout_height="wrap_content"android:layout_toRightOf="@id/plus"/><TextView android:id="@+id/equal"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="="android:layout_toRightOf="@id/num2"android:textSize="20sp"/><EditText android:id="@+id/sum"android:layout_width="100dp"android:layout_height="wrap_content"android:layout_toRightOf="@id/equal"android:textSize="20sp"android:editable="false"/><Button android:id="@+id/calculate"android:layout_width="match_parent"android:layout_height="wrap_content"android:textColor="#ffffff"android:background="@color/colorPrimary"android:layout_marginTop="80dp"android:text="計算" /></RelativeLayout>

界面

package com.android.client;import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.os.Messenger; import android.os.RemoteException; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast;public class MainActivity extends Activity implements View.OnClickListener {public static final int MSG_SUM = 1;public static final int MSG_RET = 2;public EditText num1;public EditText num2;public EditText sum;public Button btn;//客戶端信使public Messenger clientMessenger = new Messenger(new Handler(){@Overridepublic void handleMessage(Message msg) {switch (msg.what){case MSG_RET:sum.setText(msg.arg1+"");break;default:super.handleMessage(msg);}}});//服務端信使public Messenger serverMessenger;public ServiceConnection serviceConnection = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {serverMessenger = new Messenger(service);}@Overridepublic void onServiceDisconnected(ComponentName name) {serverMessenger = null;}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);num1 = (EditText) findViewById(R.id.num1);num2 = (EditText) findViewById(R.id.num2);sum = (EditText) findViewById(R.id.sum);btn = (Button) findViewById(R.id.calculate);//綁定服務Intent intent = new Intent();intent.setAction("android.intent.action.SERVICE");intent.setPackage("com.android.server");bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);btn.setOnClickListener(this);}@Overrideprotected void onDestroy() {super.onDestroy();unbindService(serviceConnection);}@Overridepublic void onClick(View v) {if(num1.getText().toString().equals("") || num2.getText().toString().equals("") ){Toast.makeText(this,"請輸入數值",Toast.LENGTH_SHORT).show();return;}Message message = Message.obtain(null,MSG_SUM,Integer.parseInt(num1.getText().toString()),Integer.parseInt(num2.getText().toString()));message.replyTo = clientMessenger;try {serverMessenger.send(message);} catch (RemoteException e) {e.printStackTrace();}} }

總結

以上是生活随笔為你收集整理的Android跨进程通信一 Messenger的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 大陆一级黄色片 | 人体av | 国产精品一区二区三区免费视频 | 无码国产精品一区二区免费16 | 国产情侣酒店自拍 | 怡红院最新网址 | 小柔的裸露日记h | 99热在线观看免费 | 国产精品卡一卡二 | 欧美大黄 | 黄色香蕉视频 | 高清国产mv在线观看 | 国产一区二区三区免费视频 | 日韩不卡的av | caopor超碰 | 国产精品乱码一区 | 亚洲一级黄色 | 蜜桃视频导航 | 97国产精品久久久 | 精品熟妇视频一区二区三区 | 九九热精品 | 日韩久久影院 | 中文字幕网址在线 | 亚洲在线播放 | 精品日韩在线观看 | 少妇精品一区二区 | 一区二区三区精品在线 | 五月激情六月 | 69视频在线观看 | 久久久老熟女一区二区三区91 | 亚洲一区区 | 欧美视频精品 | 中文幕无线码中文字蜜桃 | 日韩av不卡一区 | 久久综合久久鬼色 | 老女人人体欣赏a√s | 国产成人免费在线观看 | 国产又猛又黄又爽 | 久久久久久爱 | 久久瑟瑟 | 麻豆观看| av片在线免费观看 | 黄色网址进入 | 国产伦精品一区二区三区四区 | 97caoporn| 东京久久| 日本一级黄色大片 | 麻豆久久久午夜一区二区 | 性中文字幕 | 欧美毛片在线观看 | 中文字幕综合网 | 日韩综合色 | 亚洲经典一区二区 | 天天操人人 | 欧美顶级毛片在线播放 | 精品国产人妻一区二区三区 | 国产91一区二区三区在线精品 | 国产精品国产三级国产播12软件 | 美女高潮在线 | 黄色污网站在线观看 | 熟妇女人妻丰满少妇中文字幕 | 日本免费网站视频 | 成人黄色免费视频 | 国产中文网| 国产无码久久精品 | 麻豆69xxnxxporn | 美女乱淫 | 足交在线观看 | 性色av网址| 99热最新在线| 肉感丰满的av演员 | 欧美男优 | 男生把女生困困的视频 | 亚洲天堂色 | 久久精品噜噜噜成人88aⅴ | 艳母动漫在线播放 | 久久女人网 | 久久综合免费视频 | a天堂中文字幕 | 六月激情综合网 | 性日韩 | 色播五月激情五月 | 国产精品久久久久久久久免费看 | 国产91免费视频 | 91插插插永久免费 | 性一交一乱一色一免费无遮挡 | 日韩有码中文字幕在线 | 91精品国自产在线观看 | 一本色道久久综合亚洲精品按摩 | 中文字幕在线观看第一页 | 免费麻豆国产一区二区三区四区 | 91精品在线视频观看 | 播金莲一级淫片aaaaaaa | 色欲一区二区三区精品a片 在线观看黄网站 | 免费在线观看小视频 | 亚洲天堂最新 | 一级黄色片免费在线观看 | 亚洲视频精品在线 | 亚洲成人免费在线 |