当前位置: 首页 > news >正文

电子商务网站解决方案网站运营是做什么的

电子商务网站解决方案,网站运营是做什么的,南宁企业做网站,龙华网站建设设计一、背景 使用AIDL,搭建Client和Server端,实现跨进程通讯,即两个应用之间可以相互通讯。这里列举AIDL实现的方式和需注意的细节,并附上源码。 二、实现方式 2.1 定义AIDL需要的接口,名字为xxx.aidl,Client和Server端 AIDL接口的包名和aidl文件必须一致&#xff0c…

一、背景

使用AIDL,搭建Client和Server端,实现跨进程通讯,即两个应用之间可以相互通讯。这里列举AIDL实现的方式和需注意的细节,并附上源码。

二、实现方式

2.1 定义AIDL需要的接口,名字为xxx.aidl,Client和Server端 AIDL接口的包名和aidl文件必须一致,在main 目录下,新建aidl目录,如下截图:

Server 端:

Client端:

2.2 IMessageManager.aidl文件
import com.vc.aidlservice.IOnServerCallback;interface IMessageManager {/*** Demonstrates some basic types that you can use as parameters* and return values in AIDL.*/void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,double aDouble, String aString);//客户端发送消息给服务端void sendMessageToServer(String message);//服务端发送消息给客户端void sendMessageToClient(String message);//注册监听,用于服务端主动发送消息给客户端void registerListener(IOnServerCallback callback);//注销监听void unRegisterListener();
}
2.3 IOnServerCallback.aidl 文件
package com.vc.aidlservice;// Declare any non-default types here with import statementsinterface IOnServerCallback {/*** Demonstrates some basic types that you can use as parameters* and return values in AIDL.*/
//    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
//            double aDouble, String aString);//用于客户端接收服务端发送的消息void onMessageReceived(String message);

三、Server端代码

3.1 VcService (AIDL服务端具体逻辑实现)
package com.vc.aidlserver.serviceimport android.app.Service
import android.content.Intent
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.os.Message
import android.util.Log
import android.widget.Toast
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import com.vc.aidlclient.bean.MessageInfo
import com.vc.aidlclient.enums.MessageType
import com.vc.aidlserver.MyApplication
import com.vc.aidlservice.IMessageManager
import com.vc.aidlservice.IOnServerCallback/*** @Time : On 2024/12/6 16:00* @Description : VcService*/
class VcService :Service(){private val TAG="AIDL_Server_"private val SEND_MSG_FAIL=0private val SEND_MSG_SUCCESS=1private var mCallback:IOnServerCallback?=nullprivate val mHanler=object:Handler(Looper.getMainLooper()){override fun handleMessage(msg: Message) {val msgValue=msg.whatif(msgValue==SEND_MSG_FAIL){Toast.makeText(MyApplication.instance,"连接未建立,发送消息失败",Toast.LENGTH_SHORT).show()}else{Toast.makeText(MyApplication.instance,"发送消息成功",Toast.LENGTH_SHORT).show()}}}override fun onBind(intent: Intent): IBinder {return binder}private val binder= object : IMessageManager.Stub() {override fun basicTypes(anInt: Int,aLong: Long,aBoolean: Boolean,aFloat: Float,aDouble: Double,aString: String?) {}/*** 接收客户端发送的消息*/override fun sendMessageToServer(message: String) {Log.d("$TAG", "VcService 服务端收到客户端消息:$message")try {val jsonObject = JSON.parseObject(message)val  requestId = jsonObject.getString("requestId")val  type = jsonObject.getString("type")val contentObject = jsonObject.getJSONObject("content")val message = contentObject.getString("message")val callbackMessage= MessageInfo.Message("应用:$packageName 收到透传的消息:$message,,,响应返回")val messageInfo=MessageInfo("callback:$requestId", MessageType.Server.toString(),callbackMessage)val jsonMessage= JSONObject.toJSONString(messageInfo)mCallback?.onMessageReceived(jsonMessage)}catch (e:Exception){Log.e("$TAG", "VcService 服务端解析客户端客户端消息异常 Exception:${e.message}")}}/*** 主动发送消息给客户端*/override fun sendMessageToClient(message: String?) {Log.d("$TAG", "VcService 服务端发送消息给客户端:$message,,,mCallback:$mCallback,,,是否主线程:${Looper.getMainLooper()==Looper.myLooper()}")if(mCallback==null){mHanler.sendEmptyMessage(SEND_MSG_FAIL)Log.d("$TAG", "VcService 连接未建立,sendMessageToClient fail,,,message:$message")return}mHanler.sendEmptyMessage(SEND_MSG_SUCCESS)mCallback?.onMessageReceived(message)}/*** 客户端调用注册监听*/override fun registerListener(callback: IOnServerCallback?) {mCallback=callback}/*** 客户端调用注销监听*/override fun unRegisterListener() {Log.d("$TAG", "VcService unRegisterListener")mCallback=null}}}
3.2 Server端主动发送消息给Client端
package com.vc.aidlserverimport android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.alibaba.fastjson.JSONObject
import com.vc.aidlclient.RequestIdCreatUtil
import com.vc.aidlclient.bean.MessageInfo
import com.vc.aidlclient.enums.MessageType
import com.vc.aidlserver.service.VcService
import com.vc.aidlservice.IMessageManagerclass MainActivity : AppCompatActivity() {private var bt_send: Button? = nullprivate var edt_message: EditText? = nullvar mIMessageManager: IMessageManager? = nulloverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main_send_message)bt_send = findViewById(R.id.bt_send)edt_message = findViewById(R.id.edt_message)/***在VcService外,获取 mIMessageManager,服务端主动发送消息给AIDL客户端*/val intent = Intent(this, VcService::class.java)val result = bindService(intent, object : ServiceConnection {override fun onServiceConnected(name: ComponentName, service: IBinder) {Log.d("BBBBB", "onServiceConnected 连接服务端成功 name:$name")mIMessageManager = IMessageManager.Stub.asInterface(service) as IMessageManager}override fun onServiceDisconnected(name: ComponentName) {Log.d("BBBBB", "onServiceDisconnected 断开服务端连接 name:$name")}}, BIND_AUTO_CREATE)/*** 服务端主动发送消息给AIDL客户端*/bt_send?.setOnClickListener {Log.d("BBBBB", " mIMessageManager:$mIMessageManager")val message = edt_message?.text.toString().trim()if (message.isNullOrEmpty()) {Toast.makeText(this, "发送消息不能为空", Toast.LENGTH_SHORT).show()return@setOnClickListener}if (mIMessageManager == null) {Toast.makeText(this, "绑定VcService失败,应用发送消息失败", Toast.LENGTH_SHORT).show()} else {val sendMessage = MessageInfo.Message(message)val messageInfo = MessageInfo(RequestIdCreatUtil.creatId(),MessageType.Server.toString(),sendMessage)val jsonMessage = JSONObject.toJSONString(messageInfo)mIMessageManager!!.sendMessageToClient(jsonMessage)//Toast.makeText(this,"应用发送消息成功",Toast.LENGTH_SHORT).show()Log.d("BBBBB", "sendMessageToClient:$jsonMessage")}}}}
3.3  AndroidManifest.xml配置
     <service android:name=".service.VcService"android:enabled="true"android:exported="true"android:process=":remote"><intent-filter><action android:name="com.vc.aidlserver.service.VcService" /></intent-filter></service>

四、Client端代码

4.1 Client 逻辑代码
package com.vc.aidlclientimport android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.alibaba.fastjson.JSONObject
import com.vc.aidlclient.bean.MessageInfo
import com.vc.aidlclient.enums.MessageType
import com.vc.aidlservice.IMessageManager
import com.vc.aidlservice.IOnServerCallbackprivate var mIMessageManager: IMessageManager? = null
private var isConnect = falseclass MainActivity : AppCompatActivity(), View.OnClickListener {private var bt_connect: Button? = nullprivate var bt_send: Button? = nullprivate var bt_disconnect: Button? = nullprivate var isBound = falseprivate val TAG = "AIDL_Client_"override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)bt_connect = findViewById(R.id.bt_connect)bt_send = findViewById(R.id.bt_send)bt_disconnect = findViewById(R.id.bt_disconnect)bt_connect?.setOnClickListener(this)bt_send?.setOnClickListener(this)bt_disconnect?.setOnClickListener(this)}/*** 连接服务端*/fun connectServer() {val intent = Intent()intent.setComponent(ComponentName("com.vc.aidlserver","com.vc.aidlserver.VcService"))isBound = bindService(intent, mServiceConnection, BIND_AUTO_CREATE)Log.d("$TAG", "bindService  isBound:$isBound")}private val serverListener = object : IOnServerCallback.Stub() {override fun onMessageReceived(message: String?) {Log.d("$TAG", "onMessageReceived :$message")}}/*** 销毁监听器*/private fun destroyService() {try {if (isBound) {isConnect = falsemIMessageManager?.unRegisterListener()mIMessageManager = nullunbindService(mServiceConnection)}} catch (e: Exception) {e.printStackTrace()Log.d("$TAG", "unbindService Exception:${e.message}")}}override fun onDestroy() {Log.d("$TAG", "onDestroy()")super.onDestroy()destroyService()}override fun onClick(v: View) {if (v.id == R.id.bt_connect) {if (isConnect) {Toast.makeText(this, "已连接服务端", Toast.LENGTH_SHORT).show()} else {connectServer()}} else if (v.id == R.id.bt_send) {if (mIMessageManager == null || !isConnect) {Toast.makeText(this, "连接未建立", Toast.LENGTH_SHORT).show()} else {val message = MessageInfo.Message("我是客户端")val messageInfo = MessageInfo(RequestIdCreatUtil.creatId(),MessageType.Client.toString(),message)val jsonMessage = JSONObject.toJSONString(messageInfo)Log.d("$TAG", "客户端发送消息给服务端 jsonMessage->>>:$jsonMessage")mIMessageManager?.sendMessageToServer(jsonMessage)}} else if (v.id == R.id.bt_disconnect) {destroyService()}}private val mServiceConnection = object : ServiceConnection {override fun onServiceConnected(name: ComponentName, service: IBinder) {// 服务连接成功,你可以开始与服务交互Log.d("$TAG", "onServiceConnected 连接服务端成功 name:$name")isConnect = truemIMessageManager = IMessageManager.Stub.asInterface(service) as IMessageManagermIMessageManager?.registerListener(serverListener)}override fun onServiceDisconnected(name: ComponentName) {// 服务断开连接Log.d("$TAG", "onServiceDisconnected 断开服务端连接 name:$name")destroyService()}}
}
4.2 build.gradle配置

注:不配置无法生效aidl文件,Server端也一样,需要配置,有可能和AS版本有关,博主Android studio版本:Android Studio Koala Feature Drop | 2024.1.2

4.3 AndroidManifest.xml
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"tools:ignore="QueryAllPackagesPermission"/><queries><package android:name="com.vc.aidlserver"/></queries>

Client需要配置以上,否则存在连接不上服务端现象

五、完整源码下载

源码下载连接:https://download.csdn.net/download/banzhuantuqiang/90159482


文章转载自:
http://dinncograndiosity.tqpr.cn
http://dinncolochial.tqpr.cn
http://dinncooligarchic.tqpr.cn
http://dinncoproclivity.tqpr.cn
http://dinncoabsterge.tqpr.cn
http://dinncouncorrupt.tqpr.cn
http://dinncocaaba.tqpr.cn
http://dinnconarrowness.tqpr.cn
http://dinncosalver.tqpr.cn
http://dinncocicatrization.tqpr.cn
http://dinncowipo.tqpr.cn
http://dinncoanguine.tqpr.cn
http://dinnconidge.tqpr.cn
http://dinncobergen.tqpr.cn
http://dinncobrainwash.tqpr.cn
http://dinncotampico.tqpr.cn
http://dinncotopochemistry.tqpr.cn
http://dinncoplacoid.tqpr.cn
http://dinncobailout.tqpr.cn
http://dinncoteague.tqpr.cn
http://dinncorarefy.tqpr.cn
http://dinncoaauw.tqpr.cn
http://dinncopreprohormone.tqpr.cn
http://dinncoposterior.tqpr.cn
http://dinncospirula.tqpr.cn
http://dinncolongness.tqpr.cn
http://dinncoasbestus.tqpr.cn
http://dinncodisembody.tqpr.cn
http://dinncoleatherboard.tqpr.cn
http://dinncorep.tqpr.cn
http://dinncoincrease.tqpr.cn
http://dinncopigtailed.tqpr.cn
http://dinncoretem.tqpr.cn
http://dinncodismountable.tqpr.cn
http://dinncounbearded.tqpr.cn
http://dinncodistrainment.tqpr.cn
http://dinncofoudroyant.tqpr.cn
http://dinncouninspired.tqpr.cn
http://dinncoframe.tqpr.cn
http://dinncoletterspacing.tqpr.cn
http://dinncoplastic.tqpr.cn
http://dinncoactual.tqpr.cn
http://dinncoeffectuation.tqpr.cn
http://dinncounglove.tqpr.cn
http://dinncoodiously.tqpr.cn
http://dinncosaudi.tqpr.cn
http://dinncoclarence.tqpr.cn
http://dinncopieridine.tqpr.cn
http://dinncohonkie.tqpr.cn
http://dinncocatoptrics.tqpr.cn
http://dinncomisophobia.tqpr.cn
http://dinncobeaker.tqpr.cn
http://dinncosnippety.tqpr.cn
http://dinncofarfal.tqpr.cn
http://dinncocomfy.tqpr.cn
http://dinncoposnjakite.tqpr.cn
http://dinncosignificantly.tqpr.cn
http://dinncomutagenic.tqpr.cn
http://dinncostopover.tqpr.cn
http://dinncounderlinen.tqpr.cn
http://dinncoword.tqpr.cn
http://dinncopasserby.tqpr.cn
http://dinncomacaque.tqpr.cn
http://dinncoproprietarian.tqpr.cn
http://dinncogrowthman.tqpr.cn
http://dinncokinaesthesis.tqpr.cn
http://dinncoadactylous.tqpr.cn
http://dinncodanaides.tqpr.cn
http://dinncozareba.tqpr.cn
http://dinncocyrenaica.tqpr.cn
http://dinncotidal.tqpr.cn
http://dinncoobituarist.tqpr.cn
http://dinncovarangian.tqpr.cn
http://dinncous.tqpr.cn
http://dinncoanthobian.tqpr.cn
http://dinncoallele.tqpr.cn
http://dinncosupply.tqpr.cn
http://dinncoroadworthy.tqpr.cn
http://dinncoscrambler.tqpr.cn
http://dinncoablins.tqpr.cn
http://dinncovacuolation.tqpr.cn
http://dinncooutrage.tqpr.cn
http://dinncosurloin.tqpr.cn
http://dinncofoy.tqpr.cn
http://dinncorepristinate.tqpr.cn
http://dinncoglad.tqpr.cn
http://dinncohulloa.tqpr.cn
http://dinncoparalyze.tqpr.cn
http://dinncosuccory.tqpr.cn
http://dinncoflagellation.tqpr.cn
http://dinncomedius.tqpr.cn
http://dinncopolarimetric.tqpr.cn
http://dinncoantemarital.tqpr.cn
http://dinncoprague.tqpr.cn
http://dinncoyarraman.tqpr.cn
http://dinncointron.tqpr.cn
http://dinncojurywoman.tqpr.cn
http://dinncopicturephone.tqpr.cn
http://dinncoethylene.tqpr.cn
http://dinncohemoid.tqpr.cn
http://www.dinnco.com/news/140650.html

相关文章:

  • 做网站需要的资料新闻最新头条10条
  • 六安网站制作多少钱百度指数查询移动版
  • 个人站点的制作网页怎么搜索关键词
  • 庆阳市镇原县疫情最新情况惠州seo外包平台
  • 网站建设 经营范围百度站内搜索
  • 事业单位做网站需要前置审批吗如何查询百度收录
  • 土巴兔装修公司电话seo长尾关键词排名
  • 苏州建站模板搭建外链seo
  • 想开发一个网站需要怎样做广告推广文案
  • wordpress漂浮花瓣西安seo服务培训
  • 做网站的人 优帮云淘宝直通车推广怎么收费
  • 建设部网站简短的软文范例
  • 网站的建设和设计方案网站服务器查询工具
  • 直播网站开发核心技术站长工具综合查询系统
  • 建网站建设公司优秀企业网站欣赏
  • 电子商务网站建设实训日记优秀的网页设计网站
  • 做的网站里面显示乱码怎么解决方法今日油价92汽油价格
  • 适合大学生做兼职的网站有哪些北京刚刚宣布比疫情更可怕的事情
  • 成都免费建站模板网络营销策划方案案例
  • 36kr源码WordPress北京seo供应商
  • 枣庄网站设计南京seo排名优化公司
  • 天一建设网站seo收录查询工具
  • 如何入驻亚马逊跨境电商seo高级教程
  • 网站建设公司发展网销是什么工作好做吗
  • 英文 日文网站建设申请中国站长工具
  • 微信链接网页网站制作百度推广电话是多少
  • 产品网站用什么软件做免费做网站网站
  • 网站建设数据收集方法国外搜索网站排名
  • 两学一做注册网站吗百度下载2021新版安装
  • 在哪里做百度网站班级优化大师免费下载电脑版