无法接收来自 Activity 的广播

发布于 2025-01-03 05:27:16 字数 1375 浏览 2 评论 0原文

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

这是我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老旧海报 2025-01-10 05:27:16

我不确定这是否能解决您的问题,但我可以看到您的标签有大写“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>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文