我有一个Fragment Tabs寻呼机并使用了sherlockFragment,我需要搜索其中的其他蓝牙设备。但我无法注册接收器。我收到以下错误: 方法registerReceiver(BroadcastReceiver,IntentFilter)未定义为Fragment_Bluetooth类型。 我想知道我是否应该在另一个范围内使用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
}
}
答案 0 :(得分:1)
getActivity().registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
或getSherlockActivity()
getSherlockActivity().registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy