如何在AlertDialog Android Kotlin中从服务检索消息
我实现了一个条形码扫描程序,从条形码扫描服务中获取条形码。在这个程序中,我偶尔会打开一个带有editText字段的对话框以获取更多信息,我想知道如何让服务将条形码发送到此alertDialog 当此警报对话框关闭时,条形码应再次发送到打开的活动 以下是我如何发送/检索消息如何在AlertDialog Android Kotlin中从服务检索消息,android,kotlin,android-service,android-alertdialog,android-service-binding,Android,Kotlin,Android Service,Android Alertdialog,Android Service Binding,我实现了一个条形码扫描程序,从条形码扫描服务中获取条形码。在这个程序中,我偶尔会打开一个带有editText字段的对话框以获取更多信息,我想知道如何让服务将条形码发送到此alertDialog 当此警报对话框关闭时,条形码应再次发送到打开的活动 以下是我如何发送/检索消息 class SomeActivity : AppCompatActivity() { /* variables to communicate with scanning services */ private var
class SomeActivity : AppCompatActivity() {
/* variables to communicate with scanning services */
private var mService: Messenger? = null
private var mBound: Boolean = false
private val mMessenger: Messenger = Messenger(IncomingHandler(this))
/* service connection which binds to the scanning SDK for IPC */
private val connection: ServiceConnection = object: ServiceConnection {
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
/* connect to service*/
mService = Messenger(service)
/* Register to the scanner */
val msg: Message = Message.obtain(null, MSG_REGISTER)
msg.replyTo = mMessenger
mService?.send(msg)
}
override fun onServiceDisconnected(name: ComponentName?) {
/* In the case of unexpected disconnection */
mService = null
}
}
/* Handles inbound IPC messages and figures out what to do with them */
inner class IncomingHandler(
context: Context
) : Handler() {
override fun handleMessage(msg: Message){
when(msg.what) {
MSG_BARCODE ->{
val bcodeBundle = msg.data
val bcodeData = bcodeBundle.getString("barcode")
val bcodeType = bcodeBundle.getString("type")
barcodeScanned(bcodeData!!,bcodeType!!)
}
else ->
super.handleMessage(msg)
}
}
}
活动中的其他地方
消息制作功能
private fun getMsg(msgCode: Int) : Message{
val msg: Message = Message.obtain(null, msgCode)
msg.replyTo = mMessenger
return msg
}
发送消息示例
val newmsg: Message = Message.obtain(null, MSG_REBARCODE, 0, 0)
val bundle = Bundle()
bundle.putString("barcode",dialog.manualBarcode)
bundle.putString("type","MANUAL")
newmsg.data = bundle
mService?.send(newmsg)
绑定到服务
if (!mBound){
mBound = bindScanner(this,versionSDK, connection)
mBound = true
mService?.send(getMsg(MSG_START))
}
解开
unbindService(connection)
自定义警报对话框
在活动中调用
val dialog = CustomDialog(variousVariables...)
dialog.show()
如何创建自定义对话框
class CustomDialog (val theContext: Context, other data...)
: AlertDialog(theContext) {
init{
requestWindowFeature(Window.FEATURE_NO_TITLE)
setCancelable(false)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val window = this.window
val display = theActivity.windowManager.defaultDisplay
val size = Point()
display.getSize(size)
val width = size.x
val height = size.y
window?.setLayout(width,(height/96)*100)
setContentView(R.layout.dialog_enter_additional_data)
this.window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
this.window?.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
this.window?.setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
//Etc...
}
算出
从我的活动中我将我的messenger的值传递给我的对话框
val dialog = CustomDialog(mService, mMessenger, variousVariables...)
dialog.show()
在我的AlertDialog类中,我创建了一个新的messenger句柄,并将其应用于一个新的messenger变量
private val newMessenger: Messenger = Messenger(IncomingHandler())
/* Handles inbound IPC messages and figures out what to do with them */
inner class IncomingHandler(
) : Handler() {
override fun handleMessage(msg: Message){
when(msg.what) {
MSG_BARCODE ->{
val bcodeBundle = msg.data
val bcodeData = bcodeBundle.getString("barcode")
dataInput?.setText(bcodeData)
capturedData = bcodeData!!
}
else ->
super.handleMessage(msg)
}
}
}
然后,我使用已构建的messenger将新messenger注册到我的服务中
override fun onCreate(savedInstanceState: Bundle?) {
//Code...
val msg: Message = Message.obtain(null, MSG_REGISTER)
msg.replyTo = newMessenger
mService?.send(msg)
//Code...
}
/* Somewhere in the code */
val msg: Message = Message.obtain(null, MSG_REGISTER)
msg1.replyTo = mMessenger
service?.send(msg)
完成alertDialog后(即当它关闭时),我会将活动返回给messenger
override fun onCreate(savedInstanceState: Bundle?) {
//Code...
val msg: Message = Message.obtain(null, MSG_REGISTER)
msg.replyTo = newMessenger
mService?.send(msg)
//Code...
}
/* Somewhere in the code */
val msg: Message = Message.obtain(null, MSG_REGISTER)
msg1.replyTo = mMessenger
service?.send(msg)