Register a broadcast receiver from another broadcast receiver in android

I currently have a broadcast receiver for listening to call status events. I registered the 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 the application starts this broadcast receiver, it registers to listen to call status events and according to CALL_STATE I control my application.

It works fine until the phone restarts. After rebooting the phone, this broadcast receiver stops working. I know that I have to register a receiver to listen for the BOOT_COMPLETED system events.

What I did as 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 also gave permission to receive the BOOT_COMPLETED system event.

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

but somehow it doesn't work. I am thinking of creating a new broadcast receiver that listens only for the BOOT_COMPLETED event, but the problem is that

So my questions are: how can I run this broadcast call viewer receiver when an incoming call arrives?

How can I register a broadcast receiver from another broadcast receiver

Do I need to move the existing broadcast receiver code for maintenance so that I can get started with Boot Receiver?

Any help would be appreciated.

+5
source share
1 answer

Any other answers are welcome.

I solved this by creating a new broadcast receiver and the onReceive() method for the Broadcast receiver to be called when the phone restarts, then I will dynamically register the broadcast receiver READ_PHONE_STATE , which is also a registered registered receiver.

Below is the 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 need to register the receiver using the application context, as shown below:

 context.getApplicationContext().registerReceiver(receiver, filter); 

instead

 context.registerReceiver(receiver, filter); 

Otherwise, you will receive the following exception:

java.lang.RuntimeException: Cannot start the receiver com.ecosmob.contactpro.api.ServiceStarter: android.content.ReceiverCallNotAllowedException: BroadcastReceiver components cannot register to receive intentions

I hope this helps others!

+2
source

All Articles