将数据传递给已经具有来自 activity 的 Intent 过滤器的广播接收器
pass data to broadcast receiver that already has intent filter coming from activity
当phone连接到车内的蓝牙时。我希望我的应用程序自动打开。为此,我必须将配对的汽车蓝牙设备名称保存到一个字符串中。然后当 phones 蓝牙连接到某物时。我必须检查它是否是汽车。如果是,我想启动一项服务。
我很难将包含蓝牙汽车设备名称的字符串传递给接收器,因为我的接收器已经在接收一个 intent 过滤器来监听 ACTION_ACL_CONNECTED。是否可以将意图和意图过滤器发送到同一个接收者。
在这种情况下,如何将 btdeviceName 字符串从 activity 发送到接收方。
主要Activity
private void addDrawerItems() {
final BroadcastReceiver bluetoothBroadcast = new BluetoothReceiver();
final IntentFilter blueToothFilter = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
final Intent btbroadcastIntent = new Intent(this, BluetoothReceiver.class);
btbroadcastIntent.putExtra("btDeviceName", mPairedBluetoothDevice);
String[] osArray = {"Bluetooth Auto Start", "Reply to Calls", "Reply to sms", "Customise Message"};
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, osArray);
if (mIsPremiumUser) {
mDrawerList.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
} else {
mDrawerList.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
}
mDrawerList.setAdapter(mAdapter);
mDrawerList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
Toast.makeText(getApplicationContext(), "blue", Toast.LENGTH_LONG).show();
showBluetoothDialog();
}
return true;
}
});
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckedTextView ctv = (CheckedTextView) view;
if (!mIsPremiumUser) {
Toast.makeText(getApplication(), "Upgrade", Toast.LENGTH_LONG).show();
return;
}
switch (position) {
case 0:
if (ctv.isChecked()) {
if (!isblueToothRegistered) {
registerReceiver(bluetoothBroadcast, blueToothFilter);
sendBroadcast(btbroadcastIntent);
isblueToothRegistered = true;
}
} else {
if (isblueToothRegistered) {
unregisterReceiver(bluetoothBroadcast);
isblueToothRegistered = false;
}
}
break;
蓝牙接收器
public class BluetoothReceiver extends BroadcastReceiver {
private MainActivity ma;
private String pairedDevice;
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Receieved", Toast.LENGTH_LONG).show();
String action = intent.getAction();
pairedDevice = intent.getStringExtra("btDeviceName");
Toast.makeText(context, pairedDevice + "2", Toast.LENGTH_LONG).show();
final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
Toast.makeText(context, "Bluetooth Connected", Toast.LENGTH_LONG).show();
if (device.getName().equals(pairedDevice)) {
Toast.makeText(context, device.getName() + " 1", Toast.LENGTH_LONG).show();
}
} else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
Toast.makeText(context, "Bluetooth Disconnected", Toast.LENGTH_LONG).show();
}
}
}
一个IntentFilter
可以有多个动作。因此,首先创建您自己的自定义操作名称来监听并将其添加到 blueToothFilter
.
blueToothFilter.addAction("my.custom.action");
一旦您使用此 IntentFilter
注册 bluetoothBroadcast
接收器,它现在将接收两个操作的调用。在 onReceive
中添加另一个条件来处理您的新自定义操作。
最后在您的 Activity
中发送一个广播,其中包含您的自定义操作和准备就绪后的设备名称。
Intent intent = new Intent()
.setAction("my.custom.action")
.putExtra("btDeviceName", mPairedBluetoothDevice);
sendBroadcast(intent);
更新
我现在了解到您希望 BluetoothDevice device
和 String pairedDevice
一次调用 onReceive()
。这是不可能的,因为这些变量是从单独的操作中获取的,并且每个操作调用一次 onReceive()
。
要解决此问题,您可以将 BluetoothReceiver
更改为 Activity
的内部 class,这样您就可以保留对所需数据的引用。
当phone连接到车内的蓝牙时。我希望我的应用程序自动打开。为此,我必须将配对的汽车蓝牙设备名称保存到一个字符串中。然后当 phones 蓝牙连接到某物时。我必须检查它是否是汽车。如果是,我想启动一项服务。
我很难将包含蓝牙汽车设备名称的字符串传递给接收器,因为我的接收器已经在接收一个 intent 过滤器来监听 ACTION_ACL_CONNECTED。是否可以将意图和意图过滤器发送到同一个接收者。
在这种情况下,如何将 btdeviceName 字符串从 activity 发送到接收方。
主要Activity
private void addDrawerItems() {
final BroadcastReceiver bluetoothBroadcast = new BluetoothReceiver();
final IntentFilter blueToothFilter = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
final Intent btbroadcastIntent = new Intent(this, BluetoothReceiver.class);
btbroadcastIntent.putExtra("btDeviceName", mPairedBluetoothDevice);
String[] osArray = {"Bluetooth Auto Start", "Reply to Calls", "Reply to sms", "Customise Message"};
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, osArray);
if (mIsPremiumUser) {
mDrawerList.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
} else {
mDrawerList.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
}
mDrawerList.setAdapter(mAdapter);
mDrawerList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
Toast.makeText(getApplicationContext(), "blue", Toast.LENGTH_LONG).show();
showBluetoothDialog();
}
return true;
}
});
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckedTextView ctv = (CheckedTextView) view;
if (!mIsPremiumUser) {
Toast.makeText(getApplication(), "Upgrade", Toast.LENGTH_LONG).show();
return;
}
switch (position) {
case 0:
if (ctv.isChecked()) {
if (!isblueToothRegistered) {
registerReceiver(bluetoothBroadcast, blueToothFilter);
sendBroadcast(btbroadcastIntent);
isblueToothRegistered = true;
}
} else {
if (isblueToothRegistered) {
unregisterReceiver(bluetoothBroadcast);
isblueToothRegistered = false;
}
}
break;
蓝牙接收器
public class BluetoothReceiver extends BroadcastReceiver {
private MainActivity ma;
private String pairedDevice;
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Receieved", Toast.LENGTH_LONG).show();
String action = intent.getAction();
pairedDevice = intent.getStringExtra("btDeviceName");
Toast.makeText(context, pairedDevice + "2", Toast.LENGTH_LONG).show();
final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
Toast.makeText(context, "Bluetooth Connected", Toast.LENGTH_LONG).show();
if (device.getName().equals(pairedDevice)) {
Toast.makeText(context, device.getName() + " 1", Toast.LENGTH_LONG).show();
}
} else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
Toast.makeText(context, "Bluetooth Disconnected", Toast.LENGTH_LONG).show();
}
}
}
一个IntentFilter
可以有多个动作。因此,首先创建您自己的自定义操作名称来监听并将其添加到 blueToothFilter
.
blueToothFilter.addAction("my.custom.action");
一旦您使用此 IntentFilter
注册 bluetoothBroadcast
接收器,它现在将接收两个操作的调用。在 onReceive
中添加另一个条件来处理您的新自定义操作。
最后在您的 Activity
中发送一个广播,其中包含您的自定义操作和准备就绪后的设备名称。
Intent intent = new Intent()
.setAction("my.custom.action")
.putExtra("btDeviceName", mPairedBluetoothDevice);
sendBroadcast(intent);
更新
我现在了解到您希望 BluetoothDevice device
和 String pairedDevice
一次调用 onReceive()
。这是不可能的,因为这些变量是从单独的操作中获取的,并且每个操作调用一次 onReceive()
。
要解决此问题,您可以将 BluetoothReceiver
更改为 Activity
的内部 class,这样您就可以保留对所需数据的引用。