由于大疆 Mobile SDK 5 将 APP 注册等逻辑放到了 MyApplication 中,但是我还是想在 MainActivity 接收注册成功或失败等消息,所以使用 BroadcastReceiver 来广播消息

修改 MyApplication 代码

class MyApplication : Application() {

    companion object {
        const val REGISTER_SUCCESS = "com.example.REGISTER_SUCCESS"
        const val REGISTER_FAILURE = "com.example.REGISTER_FAILURE"
        const val PRODUCT_CONNECT = "com.example.PRODUCT_CONNECT"
        const val PRODUCT_DISCONNECT = "com.example.PRODUCT_DISCONNECT"
        const val PRODUCT_CHANGED = "com.example.PRODUCT_CHANGED"
    }

    override fun onCreate() {
        super.onCreate()

        SDKManager.getInstance().init(this, object : SDKManagerCallback {
            override fun onInitProcess(event: DJISDKInitEvent?, totalProcess: Int) {
                // ...
            }

            override fun onRegisterSuccess() {
                Log.i(TAG, "onRegisterSuccess: ")
                sendBroadcast(Intent(REGISTER_SUCCESS))
            }

            override fun onRegisterFailure(error: IDJIError?) {
                Log.i(TAG, "onRegisterFailure: $error")
                val intent = Intent(REGISTER_FAILURE).apply {
                    putExtra("error_message", error?.description) // Assuming IDJIError has a description property
                }
                sendBroadcast(intent)
            }

            // Other methods
        })
    }
}

修改 MainActivity 代码

class MainActivity : AppCompatActivity() {

    private val registerReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            when (intent.action) {
                MyApplication.REGISTER_SUCCESS -> {
                    Toast.makeText(context, "Register Success", Toast.LENGTH_SHORT).show()
                }
                MyApplication.REGISTER_FAILURE -> {
                    val errorMessage = intent.getStringExtra("error_message")
                    Toast.makeText(context, "Register Failure: $errorMessage", Toast.LENGTH_SHORT).show()
                }
                MyApplication.PRODUCT_CONNECT -> {
                    Toast.makeText(context, "Product Connect", Toast.LENGTH_SHORT).show()
                }
                MyApplication.PRODUCT_DISCONNECT -> {
                    Toast.makeText(context, "Product Disconnect", Toast.LENGTH_SHORT).show()
                }
                MyApplication.PRODUCT_CHANGED -> {
                    Toast.makeText(context, "Product Changed", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Register BroadcastReceiver
        val filter = IntentFilter().apply {
            addAction(MyApplication.REGISTER_SUCCESS)
            addAction(MyApplication.REGISTER_FAILURE)
            addAction(MyApplication.PRODUCT_CONNECT)
            addAction(MyApplication.PRODUCT_DISCONNECT)
            addAction(MyApplication.PRODUCT_CHANGED)
        }
        registerReceiver(registerReceiver, filter)
    }

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

标签: none

评论已关闭