我有两个活动,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();
}
..
..
在活动A中,我还使用以下代码注册接收器:
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>
有什么想法吗?
谢谢
答案 0 :(得分:0)
像这样创建意图:
Intent intent = new Intent();
intent.setAction(CONNECT_INTENT);
或
intent = new Intent(CONNECT_INTENT);
并且,是的,不要使用应用程序上下文,请在此站点上查看有关它的许多帖子