2013-04-22 40 views
0

我的搜索沒有讓我找到任何解決方案,也許更簡單讓我的代碼無法正常工作。無法發送意向BroadcastReceiver

我的活動javapage包含這個裏面的OnCreate:

Intent MyCountryCodeIntent = new Intent(this,OutgoingCallReceiver.class); 
MyCountryCode = "01"; 
MyCountryCodeIntent.putExtra("code",MyCountryCode); 

即使我設置startActivity(MyCountryCodeIntent);sendBroadcast(MyCountryCodeIntent); 這個應用程序掛起

我OutgoingCallReceiver.class頁面包含這個裏面的onReceive:

Bundle bundle = intent.getExtras(); 
if(null == bundle) return; 
String MyCountryCodeIntent=intent.getStringExtra("code"); 

可能是錯的,但我找不到什麼..

+0

您應該指定一個「意願行動」,而不是使用直接意圖廣播。因此,名稱「廣播」。 – RvdK 2013-04-22 09:01:08

回答

0

你必須通過意向搭上意圖這樣

Intent getextras=getIntent(); 

比你要得到特定類型的意圖一樣,如果您發送距上一次活動的String你必須抓住字符串的意圖象這樣輸入

String s= getextras.getStringExtra(name); 

simillarly

Bundle b = getextras.getBundleExtra(bundle); 
+0

非常感謝Georges – Georges 2013-04-22 12:16:12

0

sendBroadcast方法S應該用來調用BroadcastReceiver

下面的代碼將幫助你。

Intent MyCountryCodeIntent = new Intent("com.get.broadcast"); 
MyCountryCodeIntent.putExtra("code","01"); 
sendBroadcast(MyCountryCodeIntent); 

你應該採取行動com.get.broadcast

IntentFilter filter = new IntentFilter(); 
filter.addAction("com.get.broadcast"); 
registerReceiver(new OutgoingCallReceiver(),filter); 

裏面的onReceive你將能夠使用寄存器的值OutgoingCallReceiver

您應該仔細閱讀以下文章以獲取更多詳細信息。

http://mobisys.in/blog/2012/01/android-sending-receiving-broadcast-messages/

+0

非常感謝Georges – Georges 2013-04-22 12:36:13

+0

@Georges如果你有問題的解決方案,請通過接受正確的答案來解決問題。 – 2013-04-22 12:52:50

+0

我可以問你,我應該如何註冊OutgoingCallReceiver才能執行com.get.broadcast? – Georges 2013-04-22 13:09:51