2012-02-07 15 views
0

我認爲在代碼中一切都很完美,但我無法在廣播接收器中接收廣播。無法接收來自活動的廣播

這裏是我的活動

public class BroadcastDemo extends Activity { 
Button bt; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    bt = (Button)findViewById(R.id.button1); 

    bt.setOnClickListener(new OnClickListener(){ 
     public void onClick(View v) 
     { 
      Intent broadint = new Intent(); 
      broadint.setAction("SENT_BROADCAST"); 
      getApplicationContext().sendBroadcast(broadint); 
      Log.e("Dillu","BroadCast is sent"); 
     } 
    }); 
} 

}

這裏是廣播接收機

public class ReceiveBroadcast extends BroadcastReceiver{ 

@Override 
public void onReceive(Context con,Intent inte){ 
    Log.e("Dillu","Receiving the BroadCast"); 
    if(inte.getAction().equals("SENT_BROADCAST")) 
     Toast.makeText(con, "Received BroadCast Succesfully", Toast.LENGTH_LONG).show(); 
     Log.e("Dillu","Received the BroadCast"); 
} 

}

這裏是清單文件中註冊的接收器

<Receiver 
     android:name=".ReceiveBroadcast"> 
     <intent-filter> 
      <action android:name="SENT_BROADCAST"></action> 
     </intent-filter> 
    </Receiver> 

廣播接收器無法接收從活動發送的廣播。我不知道我出錯的地方。

+0

是否設置所需的權限? – Stuti 2012-02-07 13:17:01

+0

@Stuti - 活動和廣播接收器都在同一個應用程序中。我應該保留哪些權限? – 2012-02-07 13:24:24

回答

1

我不知道這會解決你的問題,但我可以看到你的標籤有大寫字母「R」

<Receiver>標籤應該是<receiver>

+1

非常感謝你。它現在工作得很好。編寫R而不是r是一個非常大的錯誤。 – 2012-02-07 13:42:51