Broadcast Receiver
A Broadcast receiver is an android component which allows you to register for the system or application events.The Broadcast receiver's job is to pass a notification to the user, in case a specific event occur.For example, application can register for the ACTION_BOOT_COMPLETED system event which is fired once the Android system has completed the boot process.
Registration For Broadcast Receiver
There are two ways to register a Broadcast Receiver.
Static -The broadcast receiver register in AndroidManifest.xml file with <receiver> tag.
in which the broadcast receiver is registered in an android application via AndroidManifest.xml file.
<receiver | |
android:name=".MyPhoneStateListener" | |
android:enabled="true" | |
android:exported="true" > | |
<intent-filter> | |
<action android:name="android.intent.action.PHONE_STATE" /> | |
</intent-filter> | |
</receiver> |
Dynamic- The broadcast receiver register in activity file(.java). we can register the broadcast receiver by using Context.registerReceiver() method.Dynamically registered broadcast receivers can be unregistered by using Context.unregisterReceiver() method.
The way to register Dynamic Broadcast receiver :
The way to register Dynamic Broadcast receiver :
BroadcastReceiver mReceiver=new MyBroadcastReceiver(); registerReceiver( this.myReceiver, new IntentFilter("Event Name"));
IntentFilter object play main role that specify which event/intent our receiver will listen to.
Here is example for Listen Phone state using Broadcast Receiver.
activity_main .xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
tools:context="com.androidheight.broadcast.MainActivity"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Hello World!" /> | |
</RelativeLayout> |
MainActivity.java
package com.androidheight.broadcast; | |
import android.content.Context; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.Menu; | |
public class MainActivity extends AppCompatActivity { | |
static Context context; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
context=this; | |
} } | |
Here is broadcast receiver class
MyPhoneStateListener .java
package com.androidheight.broadcast; import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.telephony.PhoneStateListener; | |
import android.telephony.TelephonyManager; | |
import android.widget.Toast; | |
public class MyPhoneStateListener extends BroadcastReceiver{ | |
@Override | |
public void onReceive(Context mContext, Intent intent) { | |
try { | |
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); | |
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { | |
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); // 5 | |
String msg = "Incoming number is " + incomingNumber; | |
Toast.makeText(mContext, " your Phone Is Ringing\n"+msg, Toast.LENGTH_LONG).show(); | |
} | |
if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) | |
{ | |
Toast.makeText(mContext, "Call Recieved", Toast.LENGTH_LONG).show(); | |
// Your Code | |
} | |
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) | |
{ | |
Toast.makeText(mContext, "Phone Is Disconnected or Idle", Toast.LENGTH_LONG).show(); | |
// Your Code | |
} | |
} | |
catch(Exception e) | |
{ | |
//your custom message | |
} | |
} | |
} |
OUTPUT
Download code from GitHub
0 comments:
Post a Comment