Introduction
This article explains how to broadcast a message after the phone changes its state in Android. Android Studio is used to develop the sample.
This application will Broadcast a message when a call comes in the Android device.
- Click on the file
- click on new project
- click on Android application project
- Write the name of you application in the box depending on your project

Step 2
Register a Broadcast Receiver in your Android Manifest.xml file like the following;
for this you will use the "receiver" tag:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.broadcastphoneste"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="17" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.broadcastphoneste.MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <receiver
- android:name="MyReciever">
- <intent-filter>
- <action android:name="android.intent.action.PHONE_STATE"/>
- </intent-filter>
- </receiver>
- </application>
- <uses-permission
- android:name="android.permission.READ_PHONE_STATE"></uses-permission>
- </manifest>
Step 3
Create an XML file and write the following.
This XML file contains a TextView that displays a message when the application runs.
- <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:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin"
- tools:context=".MainActivity">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="WhenEver Call comes it will show a Message" />
- </RelativeLayout>
Step 4
Create a MainActivty.java class and write this:
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
Step 5
Create another Java class that extends the BroadcastReceiver class as in the following:
Run this application on the emulator
First run the application; after displaying the activity page you will open the android DDMS where you click on the Emulator Control that will display the page like this:
In the middle of this page, you will see the incoming number where you will enter the number and click on the send button. Then you will see on your activity a broadcast message will appear.
- package com.broadcastphoneste;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.telephony.PhoneStateListener;
- import android.telephony.TelephonyManager;
- import android.util.Log;
- import android.widget.Toast;
- /**
- * Created by vivek on 10/3/13.
- */
- public class MyReciever extends BroadcastReceiver {
- Context context;
- @Override
- public void onReceive(Context c, Intent intent) {
- context=c;
- try {
- // TELEPHONY MANAGER class object to register one listner
- TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
- //Create Listner
- MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
- // Register listener for LISTEN_CALL_STATE
- telephonyManager.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
- } catch (Exception e) {
- Log.e("Phone Receive Error", " " + e);
- }
- }
- private class MyPhoneStateListener extends PhoneStateListener {
- public void onCallStateChanged(int state, String incomingNumber) {
- Log.d("MyPhoneListener",state+" incoming no:"+incomingNumber);
- if (state == 1) {
- String msg = "New Phone Call Event. Incomming Number : "+incomingNumber;
- Toast.makeText(context,msg,Toast.LENGTH_LONG).show();
- }
- }
- }
- }

Step 6




Join the conversation! Your thoughts help the community grow.