无法从活动中接收广播

时间:2012-02-07 13:12:54

标签: android broadcast

我认为代码中的一切都很完美,但我无法在广播接收器中接收广播。

这是我的活动

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

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

广播接收器无法接收从活动发送的广播。我不知道我哪里出错了。

1 个答案:

答案 0 :(得分:1)

我不确定这会解决您的问题,但我可以看到您的代码有资本&#34; R&#34;

<Receiver>标记应为<receiver>