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.
If the receiving class is not registered using in its manifest, you can dynamically instantiate and register a receiver by calling Context. registerReceiver(). Take a look at registerReceiver (BroadcastReceiver receiver, IntentFilter filter) for more info.
A BroadcastReceiver can be registered in two ways. By defining it in the AndroidManifest. xml file as shown below.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With