Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法理解intent.getaction();_Android_Bluetooth - Fatal编程技术网

Android 无法理解intent.getaction();

Android 无法理解intent.getaction();,android,bluetooth,Android,Bluetooth,上述代码用于扫描Bluetooth设备并将其添加到mArrayAdapter中。 但是,我不理解这些行String action=intent.getAction 如果找到BluetoothDevice.ACTION\u.equalsaction 有人能简要解释一下吗。我已经搜索过了,但我不明白intent.getaction的用法。它返回什么?值是如何设置的?嗯,它实际上非常简单 每一个意图都只是一个信息。但是,你可以将意图从一个应用发送到另一个应用。因此,在这种情况下,当您从另一个appbl

上述代码用于扫描Bluetooth设备并将其添加到mArrayAdapter中。 但是,我不理解这些行String action=intent.getAction

如果找到BluetoothDevice.ACTION\u.equalsaction


有人能简要解释一下吗。我已经搜索过了,但我不明白intent.getaction的用法。它返回什么?值是如何设置的?

嗯,它实际上非常简单

每一个意图都只是一个信息。但是,你可以将意图从一个应用发送到另一个应用。因此,在这种情况下,当您从另一个appbluetooth控制器接收到意图时,您需要知道它的含义。这基本上就是行动的目的

而且,由于广播接收器通常可以接收几种不同类型的意图,“如果”确保您刚刚收到的意图确实意味着找到了蓝牙设备

我建议您阅读这篇关于意图和意图过滤器的文章:

编辑:正如pomber所建议的,在您的情况下,它确实是不必要的。

在您的情况下,if BluetoothDevice.ACTION\u FOUND.equalsaction是不必要的,因为您只需要将该广播接收器注册到筛选器new IntentFilterBluetoothDevice.ACTION\u FOUND

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    // When discovery finds a device
    if (BluetoothDevice.ACTION_FOUND.equals(action)) {
        // Get the BluetoothDevice object from the Intent
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        // Add the name and address to an array adapter to show in a ListView
        mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
    }
 }
};
// Register the BroadcastReceiver
   IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
   registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy`