android - 如何使用 Intent 中的附加功能

标签 android android-intent bluetooth broadcastreceiver intentfilter

我正在学习如何在 android 上使用蓝牙,并且我为 BluetoothAdapter.ACTION_STATE_CHANGED 注册了一个 BroadCastReceiver。 在文档中,它说

Broadcast Action: The state of the local Bluetooth adapter has been changed. For example, Bluetooth has been turned on or off. Always contains the extra fields EXTRA_STATE and EXTRA_PREVIOUS_STATE
containing the new and old states respectively.

现在,我如何使用 EXTRA_STATE 和 EXTRA_PREVIOUS_STATE?

最佳答案

来自 here :

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();

    if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
        final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                                             BluetoothAdapter.ERROR);
        switch (state) {
        case BluetoothAdapter.STATE_OFF:
            //Bluetooth off
            break;
        case BluetoothAdapter.STATE_TURNING_OFF:
            //Turning Bluetooth off...
            break;
        case BluetoothAdapter.STATE_ON:
            //Bluetooth on
            break;
        case BluetoothAdapter.STATE_TURNING_ON:
            //Turning Bluetooth on...
            break;
        }
    }
}

};

关于android - 如何使用 Intent 中的附加功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32168169/

相关文章:

java - 如何在一个应用程序中的多个 Activity 之间共享蓝牙接收的数据

android - 打开通知面板时通知声音停止

Android 页脚固定在底部并将位于其他 View 之上

android - 使用 Intents for samsung explorer 打开指定文件夹

Android - 创建和运行快捷方式

java - 在 Android 设备之间传输文件?

android - 更改 Android Things Activity 的方向

java - VideoView onTouch 事件 : pause/resume video, 和显示/隐藏 MediaController 和 ActionBar

java - 如何将 TextView 声明为全局变量以在其他类中使用

ios - 如何在 Objective C 中创建 CBUUID