2013-05-07 54 views
0

我有兩項活動,A和B.活動A用廣播接收器捕獲意圖,活動B觸發它。活性A的未收到定製意圖

代碼:

​​

在活動AI也註冊用下面的代碼的接收器:

活性B的
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
filter.addAction(floatyNetworkList.CONNECT_INTENT); 
registerReceiver(mReceiver, filter); 

代碼,我的意圖進行燒成時,我選擇的項目我的列表視圖:

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

你註冊你的接收器嗎? – pskink 2013-05-07 14:38:29

+0

您可以向我們展示BroadcastReceiver註冊的代碼或聲明的代碼 – k3b 2013-05-07 14:38:31

+0

您可以發佈您的清單嗎? – 2013-05-07 14:38:38

回答

0

創建的意圖是這樣的:

Intent intent = new Intent(); 
intent.setAction(CONNECT_INTENT); 

intent = new Intent(CONNECT_INTENT); 

,是的,不使用應用程序上下文,看到很多關於它的帖子在本網站

+0

現在好了它的工作原理,但我只是改變了意圖'Intent intent = new Intent();'不修改上下文:)。 – 2013-05-08 09:06:51

+0

好吧,我同意它的作品,但閱讀rhis:http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context – pskink 2013-05-08 09:24:05

+0

非常感謝你分享這個,現在我根據你的回覆改變了代碼:) – 2013-05-08 10:16:19