0

我有一个 Fragment Tabs Pager 并使用了 sherlockFragment,我需要在其中搜索其他蓝牙设备。但我无法根据需要注册Receiver。我收到以下错误:Fragment_Bluetooth 类型的方法 registerReceiver(BroadcastReceiver, IntentFilter) 未定义。我想知道是否应该在另一个范围内使用 registerReceiver ?在哪里以及如何?这是我的代码:

public class Fragment_Bluetooth extends SherlockFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Create a BroadcastReceiver for ACTION_FOUND
        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 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
    }
}
4

1 回答 1

1
 getActivity().registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy

或 getSherlockActivity()

 getSherlockActivity().registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
于 2013-06-09T20:20:00.783 回答