簡體   English   中英

無法使用廣播接收器 (Kotlin) 更新 UI

[英]Unable to update the UI using Broadcast Receiver(Kotlin)

當藍牙打開/關閉時,我想更新我的 MainActivity 的 UI

主要活動

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

BroadcastReceiver

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 方法中藍牙值返回 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))
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM