如何从 AlertDialog Android Kotlin 中的服务检索消息

How to retrieve messages from a service in an AlertDialog Android Kotlin

我实现了一个条码扫描程序,它从条码扫描服务中获取条码。在这个程序中,我偶尔会打开一个带有 editText 字段的对话框以获取更多信息,我想知道如何获得将条形码发送到此 alertDialog 的服务。

当此 alertDialog 关闭时,条形码应再次发送到打开的 activity。

这是我Send/Retrieve消息

的方式
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)
            }
        }
    }

在 activity

的其他地方

消息制作功能

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)

自定义 AlertDialog 在 activity

内调用
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...
}

想通了

从我的 Activity 我将 Messenger 的值传递到我的 Dialog

val dialog = CustomDialog(mService, mMessenger, variousVariables...)
dialog.show()

在我的 AlertDialog Class 中,我创建了一个新的信使句柄并将其应用到一个新的信使变量

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...
}

一旦我完成了我的 alertDialog(即当它关闭时),我将 activity 还给它的信使

/* Somewhere in the code */
val msg: Message = Message.obtain(null, MSG_REGISTER)
            msg1.replyTo = mMessenger
            service?.send(msg)