Sending and receiving broadcast from the same receiver - android

Hello I was trying to implement a custom Broadcast receiver for my Geofence app. I just went through the solution given here But I found that he is sending the broadcast from the receiver class which receives the same broadcast. can someone please tell me how this works. I have not worked much on custom broadcast.

He is sending the broadcast from one class and receiving it in another receiver. The line below is where he sends out the broadcast.
Intent intent = new Intent("com.aol.android.geofence.ACTION_RECEIVE_GEOFENCE");
Here is his manifest where he registers a receiver for that broadcast
<receiver android:name="com.aol.android.geofence.GeofenceReceiver"
android:exported="false">
<intent-filter >
<action android:name="com.aol.android.geofence.ACTION_RECEIVE_GEOFENCE"/>
</intent-filter>
</receiver>

you can send one Broadcast in another one with following code:
ntent local = new Intent();
local.setAction("BroadCastPath"); // like android.receiver.MyReceiver
context.sendBroadcast(local);

Related

Broadcast receiver not invoked

I am having problems with my Broadcast not being received.
I have tried using a standard broadcast and a localbroadcast and neither will work.
This is the broadcast code:
Intent intent = new Intent();
intent.setAction(WIFI_SKIPPED_INTENT);
context.sendBroadcast(intent);
where WIFI_SKIPPED_INTENT = "com.wefi.wifi.skip"
And this is in my manifest:
<receiver
android:name="com.wefi.receivers.WifiSkipReceiver"
android:exported="true"
android:enabled="true">
<intent-filter>
<action android:name="com.wefi.wifi.skip" />
</intent-filter>
</receiver>
I added log commands before the sending and they are showing but logs in the receiver are not.
This happens on Android 11, Android 10 and Android 8 phones.
Edit:
If we register dynamically fro the receiver within the code then it works great. It fails only when registering through the manifest.

Receiving Broadcast Sent From Library Module in App Module

I am having problem receiving broadcast sent from android library module in app module.
I create an explicit broadcast with action and component name. And send this broadcast from android lib module.
val intent = Intent()
intent.action = "com.example.action.SOME_ACTION"
intent.component = ComponentName("com.example", "com.example.MyReceiver")
sendBroadcast(intent)
To receive this broadcast, I created a receiver and registered it in manifest.
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.action.SOME_ACTION" />
</intent-filter>
</receiver>
This receiver prints received event, but unfortunately, I am not receiving any broadcast. Am I missing anything? Or is it supposed to be this way, i.e. You cannot receive broadcast sent from the library?
try to do like this, this is working for me
register receiver where you want to receive using this code
IntentFilter filter = new IntentFilter();
filter.addAction("myCustomAction");
registerReceiver(receiver, filter);
send a broadcast from different lib like this
Intent intent = new Intent("myCustomAction");
intent.putExtra("value", 0);
intent.setComponent(null);
context.sendBroadcast(intent);
now no need to register a receiver in AndroidManifest.xml
set ComponentName to null because ComponentName is
The name of the application component to handle the
intent, or null to let the system find one for you.

Register Broadcast Receiver from another Broadcast Receiver in android

Currently I have Broadcast Receiver for listening call states events. I have registered Broadcast Receiver in AndroidManifest.xml as shown below.
<receiver android:name=".api.PhoneCallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
When Application launches this broadcast receiver is registered for listening call states events and according to CALL_STATE i am managing my application.
It is working fine until phone restarts.
After phone restart this broadcast receiver stops working. I know I have to register receiver for listening BOOT_COMPLETED event of system.
What i have done is like shown below:
<receiver android:name=".api.PhoneCallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
I have also given Permission for getting BOOT_COMPLETED system event.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
but somehow it is not working. I am thinking of making new Broadcast Receiver that listens for BOOT_COMPLETED event only but issue is that
So my questions is how can i start this Phone Call Listener Broadcast Receiver when any incoming call comes in?
How can i register Broadcast Receiver from another Broadcast Receiver
Do i have to move my existing broadcast receiver's code to service so i can start service from Boot Receiver?
Any help will be appreciated.
Any Other answers are welcome.
I have solved it by creating new Broadcast receiver and onReceive() method of that Broadcast receiver will be called when phone restarts then i have dynamically register READ_PHONE_STATE broadcast receiver that is also manifest registered receiver.
Below is code:
AndroidManifest.xml:
<receiver android:name=".api.ServiceStarter">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
BroadcastReceiver:
public class ServiceStarter extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.PHONE_STATE");
PhoneCallReceiver receiver = new PhoneCallReceiver();
context.getApplicationContext().registerReceiver(receiver, filter);
}
}
You have to register receiver using Application context like below:
context.getApplicationContext().registerReceiver(receiver, filter);
instead of
context.registerReceiver(receiver, filter);
Otherwise you will get following exception:
java.lang.RuntimeException: Unable to start receiver
com.ecosmob.contactpro.api.ServiceStarter:
android.content.ReceiverCallNotAllowedException: BroadcastReceiver
components are not allowed to register to receive intents
I hope it helps others!

Dynmaic registration of BroadcastReceiver's actions (intent-filter)

I have a BroadcastReceiver and a Service in my application. I am receiving the action information in the service from an activity. I have to register this action dynamically with the receiver. I have a few actions defined in the AndroidManifest.xml, but this action has to be defined dynamically. Can someone please help me with this?
Thanks.
P.S. A sample receiver tag from the AndroidManifest.xml. BOOT_COMPLETED is one of the actions I am referring to. I am receiving this information in this service.
<receiver android:name="com.test.TestReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
You can use this:
IntentFilter intentFilter = new IntentFilter("android.intent.action.BOOT_COMPLETED");
YourReceiver receiver = new YourReceiver();
LocalBroadcastManager mBroadcastMgr = LocalBroadcastManager
.getInstance(getApplicationContext());
mBroadcastMgr.registerReceiver(receiver, intentFilter);

unable to receive ACTION_TIME_CHANGED intent

I am not able to receive the ACTION_TIME_CHANGED intent in my broadcast receiver class when I change the time from the phone. Is there anything in particular that I need to add in android.manifest?
I have already added
in broadcast receiver class:
else if((intent.getAction().equals(Intent.ACTION_TIME_CHANGED)) ||(intent.getAction().equals(Intent.ACTION_DATE_CHANGED ))){
Log.d("ContactsAppReceiver", "ACTION_TIME_CHANGED");
}
And in manifest.xml file. :
<action android:name="android.intent.action.ACTION_TIME_CHANGED"/>
Add to manifest:
<action android:name="android.intent.action.TIME_SET" />

Categories

Resources