无法接收来自 Activity 的广播
我认为代码中的一切都很完美,但我无法在广播接收器中接收广播。
这是我的 ACTIVITY
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");
}
}
这是在 Manifest 文件中注册的接收器
<Receiver
android:name=".ReceiveBroadcast">
<intent-filter>
<action android:name="SENT_BROADCAST"></action>
</intent-filter>
</Receiver>
广播接收器无法接收从 Activity 发送的广播。我不知道我哪里错了。
I think everything is perfect in the code but I'm not able to receive the broadcast in the broadcast receiver.
Here is my ACTIVITY
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");
}
});
}
}
Here is Broadcast Receiver
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");
}
}
Here is the Receiver registered in the Manifest file
<Receiver
android:name=".ReceiveBroadcast">
<intent-filter>
<action android:name="SENT_BROADCAST"></action>
</intent-filter>
</Receiver>
The Broadcast receiver is not able to receive the Broadcast sent from Activity. I don't know where I have gone wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定这是否能解决您的问题,但我可以看到您的标签有大写“R”
标签应该是I am not sure this would resolve your question, but I can see that your tag has capital "R"
<Receiver>
tag should be<receiver>