A broadcast receiver is a component that responds to system-wide broadcast announcements. Many broadcasts originate from the system—for example, a broadcast announcing that the screen has turned off, the battery is low, or a picture was captured.
There are two ways to use broad cast receiver in android --
Register broadcast receiver Via dynamically ( Run time )
Register broadcast receiver Via the AndroidManifest.xml file.
Now Create PhoneCall.java file
- One is using dynamically in activity
- Second to register through manifest
Register broadcast receiver Via dynamically ( Run time )
// Create reciever object private BroadcastReceiver the_receiver = new PhoneCall(); // Set When broadcast event will fire. private IntentFilter filter = new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED); // Register new broadcast receiver this.registerReceiver(the_receiver, filter); // Reciever class which extends BroadcastReceiver public class PhoneCall extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { // Phone call state change then this method will automaticaly called } }Register through manifest
Register broadcast receiver Via the AndroidManifest.xml file.
When android.intent.action.PHONE_STATE Event will fire ( call recieved/pick/end ), then onReceive method in PhoneCall.java file will automaticaly call.
Now Create PhoneCall.java file
// Reciever class which extends BroadcastReceiver public class PhoneCall extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { // Phone call state change then this method will automaticaly called }}
Comments
Post a Comment