在`BroadcastReceiver`内部发送另一个广播不起作用?

huangapple go评论53阅读模式
英文:

Sending another Broadcast inside BroadcastReceiver not working?

问题

可能从一个 BroadcastReceiver 发送广播到另一个 BroadcastReceiver 吗?

我可以完美运行第一个广播接收器,使用以下调用:

Intent intent = new Intent(CalendarAlarmManager.ACTION_CHECK_NEXT_ALARM);
intent.setClass(context, CalendarProviderBroadcastReceiver.class);
context.sendBroadcast(intent);

在第一个接收器中,我执行一些操作,然后运行第二个广播接收器:

Intent intent = new Intent(ACTION_EVENT_REMINDER);
intent.setClass(context, AlarmReceiver.class);
context.sendBroadcast(intent);   // 方法被调用

AndroidManifest 中:

        <receiver android:name=".CalendarProviderBroadcastReceiver"
            android:exported="true">
            <intent-filter>
                <action android:name="com.example.SOMETHING.TEST" />
            </intent-filter>
        </receiver>

        <receiver android:name=".AlarmReceiver"
            android:exported="false">
            <intent-filter>
                <action android:name="com.exmaple.SOMETHING.REMIND_ME" />
            </intent-filter>
        </receiver>

但令人惊讶的是,第二个接收器的 onReceive 方法没有被调用!在 Android 中发送广播从另一个广播接收器是否有任何限制?

英文:

Is it possible to send a broadcast from one BroadcastReceiver to another BroadcastReceivers?

I am running first broadcast receiver perfectly with below call:

Intent intent = new Intent(CalendarAlarmManager.ACTION_CHECK_NEXT_ALARM);
intent.setClass(context, CalendarProviderBroadcastReceiver.class);
context.sendBroadcast(intent);

In first receiver, I'm doing some stuff and then running second broadcast receiver:

Intent intent = new Intent(ACTION_EVENT_REMINDER);
intent.setClass(context, AlarmReceiver.class);
context.sendBroadcast(intent);   // method is being called

Inside AndroidManifest:

        &lt;receiver android:name=&quot;.CalendarProviderBroadcastReceiver&quot;
            android:exported=&quot;true&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;com.example.SOMETHING.TEST&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/receiver&gt;

        &lt;receiver android:name=&quot;.AlarmReceiver&quot;
            android:exported=&quot;false&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;com.exmaple.SOMETHING.REMIND_ME&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/receiver&gt;

But surprisingly second receiver's onReceive method is not being called! Is ther any limitation in android for sending broadcast from another broadcast receiver?

答案1

得分: 0

答案是。可以从广播发送另一个广播,没有限制。

我只是将AndroidManifest从这里更改:

        &lt;receiver android:name=&quot;.AlarmReceiver&quot;
            android:exported=&quot;false&quot;&gt;  &lt;--- 这一行是问题所在
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;com.exmaple.SOMETHING.REMIND_ME&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/receiver&gt;

到这里:

        &lt;receiver android:name=&quot;.AlarmReceiver&quot;
            android:exported=&quot;true&quot;&gt;  &lt;--- 现在已经修复了
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;com.exmaple.SOMETHING.REMIND_ME&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/receiver&gt;

我的代码问题是,我的广播被定义在除了App module之外的模块中。也许因为这个原因,Android认为它是其他应用程序的接收器。

英文:

The answer is yes. There is no limitation in sending another broadcast from a broadcast.

I simply changed AndroidManifest from this:

        &lt;receiver android:name=&quot;.AlarmReceiver&quot;
            android:exported=&quot;false&quot;&gt;  &lt;--- This line is the problem
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;com.exmaple.SOMETHING.REMIND_ME&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/receiver&gt;

to this:

        &lt;receiver android:name=&quot;.AlarmReceiver&quot;
            android:exported=&quot;true&quot;&gt;  &lt;--- It is fixed now.
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;com.exmaple.SOMETHING.REMIND_ME&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/receiver&gt;

The problem with my code is that my broadcasts are defined inside a module other than App module. And maybe because of that, android is considering it as other application's receiver.

huangapple
  • 本文由 发表于 2023年3月7日 16:53:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659772.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定