android - 无法使用 Broadcast Receiver(Kotlin) 更新 UI

标签 android kotlin

我想在蓝牙打开/关闭时更新 My MainActivity 的 UI

主 Activity

private val broadcastReceiver = Broadcast()
    open var bluetooth : ImageView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val filter = IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)
        registerReceiver(broadcastReceiver, filter)
        val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
        bluetooth = findViewById(R.id.image_bluetooth)
        val bluetoothName = findViewById<TextView>(R.id.text_bluetooth_name)
        bluetoothName.text = bluetoothAdapter.name
        bluetooth?.setOnClickListener {
            if (!bluetoothAdapter.isEnabled) {
                val turnOn = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
                startActivityForResult(turnOn, 0)
            } else {
                bluetoothAdapter.disable()
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(broadcastReceiver)
    }

    fun enableBluetooth()
    {
        bluetooth?.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.ic_bluetooth_enable_48dp))
    }
    fun disableBluetooth()
    {
        bluetooth?.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.ic_bluetooth_disable_48dp))
    }

广播接收器

class Broadcast : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            val action = intent?.action
            val mainActivity = MainActivity()
            if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
                when (intent?.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
                    BluetoothAdapter.STATE_OFF -> {
                        Toast.makeText(context, "Bluetooth Turned OFF", Toast.LENGTH_LONG).show()
                        mainActivity.disableBluetooth()
                    }
                    BluetoothAdapter.STATE_ON -> {
                        Toast.makeText(context, "Bluetooth Turned ON", Toast.LENGTH_LONG).show()
                        mainActivity.enableBluetooth()
                    }
                }

            }
        }
    }

在 BroadcastReceiver 中的 onReceive 调用之后,在 enableBluetooth 和 disableBluetooth 方法中 bluetooth 值返回 null。任何人都可以帮助完成这个过程吗?提前致谢

最佳答案

对于这种情况,使用OnActivityResult方法来更新UI

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 0 && bluetoothAdapter!!.isEnabled) {
            Toast.makeText(this, "Bluetooth Turned ON", Toast.LENGTH_LONG).show()
            image_bluetooth?.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_bluetooth_enable_48dp))
        }
    }

关于android - 无法使用 Broadcast Receiver(Kotlin) 更新 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58659838/

相关文章:

android - 修改重量在 jetpack compose 1.1.1 中不可用

swift - Kotlin 是否支持像 swift 这样的可选构造函数?

android - Jetpack 撰写 : Scaffold drawer opens when dragging MapView

android - 如何格式化要发送到 BLE 设备上的特征的字节数组?

android - Ionic 3 版本未更新——Android/Mac

java - View 索引到底是什么以及当 View 组将 View 添加到索引 -1 时会发生什么

android - 如何在 imageview android 中显示大尺寸位图?

android - 如何将cgal库移植到android

Java,根据所选州获取城市

json - 如何使用 Gson 反序列化继承的 Kotlin 数据类