我有两个活动,A 和 B。活动 A 用广播接收器捕获意图,活动 B 触发它。
活动代码 A:
final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(floatyNetworkList.CONNECT_INTENT)){
Log.v("com.vittorio", "intent catched");
BluetoothDevice target = (BluetoothDevice) intent.getExtras().get("Device");
Thread T = new Thread(new ConnectThread(target, mHandler, recmsgs, DDB, "Connecting"));
T.start();
}
..
..
在活动 AI 中,还使用以下代码注册接收器:
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
filter.addAction(floatyNetworkList.CONNECT_INTENT);
registerReceiver(mReceiver, filter);
活动 B 的代码,当我从列表视图中选择一个项目时,我的意图被触发:
listBtview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterBt, View myView, int myItemInt, long mylng) {
@SuppressWarnings("unchecked")
HashMap<String,String> selectedFromList =(HashMap<String,String>) (listBtview.getItemAtPosition(myItemInt));
Toast.makeText(getBaseContext(), selectedFromList.get("Name"), Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), Floaty.class);
intent.setAction(CONNECT_INTENT);
intent.putExtra("Device",fetchDevice(selectedFromList.get("Address")));
getApplicationContext().sendBroadcast(intent);
}
});
}
问题是活动 A 永远不会收到意图。我还为我的自定义意图添加了过滤器并正确更新了我的清单。
清单代码:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="com.vittorio.intent.action.CONNECT_INTENT"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
有任何想法吗?
谢谢