【问题标题】:How to access value of an extra如何访问额外的价值
【发布时间】:2015-08-24 07:30:56
【问题描述】:

我想访问包含在 BluetoothDevice.ACTION_FOUND 中的额外内容,例如,如果我想访问包含在 BluetoothDevice.EXTRA_CLASS 中的值,我应该使用它,如下面的代码所示,但是当我显示变量“状态”,类似于 -245175

BluetoothDevice.EXTRA_CLASS 中是否还有其他值?我应该怎么认识他们?我应该如何访问它们?

更新

我注册的意图是:BluetoothDevice.ACTION_FOUND

代码

switch (action) {
        case BluetoothDevice.ACTION_FOUND:
            Log.d(TAG, LogAnd.i("onReceive", "BluetoothDevice.ACTION_FOUND"));

            final int state = intent.getIntExtra(BluetoothDevice.EXTRA_CLASS, BluetoothAdapter.ERROR);
            Log.d(TAG, LogAnd.i("onReceive", "state: "+state));
            break;

【问题讨论】:

    标签: android android-intent broadcastreceiver intentfilter extras


    【解决方案1】:

    您使用的是getIntExtra(),但额外的值不是整数。它仍然“有效”,但内容以错误的方式解释,所以你得到一个毫无意义的值。

    docs for EXTRA_CLASS 说:

    在 ACTION_FOUND 和 ACTION_CLASS_CHANGED 意图中用作 Parcelable BluetoothClass 额外字段。

    您应该改用getParcelableExtra()。那应该给你一个正确类的实例。像这样:

    BluetoothClass bclass = (BluetoothClass)intent.getParcelableExtra(BluetoothDevice.EXTRA_CLASS);
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-11
    相关资源
    最近更新 更多