英文:
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 BroadcastReceiver
s?
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
:
<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>
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
从这里更改:
<receiver android:name=".AlarmReceiver"
android:exported="false"> <--- 这一行是问题所在
<intent-filter>
<action android:name="com.exmaple.SOMETHING.REMIND_ME" />
</intent-filter>
</receiver>
到这里:
<receiver android:name=".AlarmReceiver"
android:exported="true"> <--- 现在已经修复了
<intent-filter>
<action android:name="com.exmaple.SOMETHING.REMIND_ME" />
</intent-filter>
</receiver>
我的代码问题是,我的广播被定义在除了App module之外的模块中。也许因为这个原因,Android认为它是其他应用程序的接收器。
英文:
The answer is yes. There is no limitation in sending another broadcast from a broadcast.
I simply changed AndroidManifest
from this:
<receiver android:name=".AlarmReceiver"
android:exported="false"> <--- This line is the problem
<intent-filter>
<action android:name="com.exmaple.SOMETHING.REMIND_ME" />
</intent-filter>
</receiver>
to this:
<receiver android:name=".AlarmReceiver"
android:exported="true"> <--- It is fixed now.
<intent-filter>
<action android:name="com.exmaple.SOMETHING.REMIND_ME" />
</intent-filter>
</receiver>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论