How to Broadcast a Message After a Phone Change Its State in Android Using Android Studio

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.
 
To make this type of application you need to first register your Broadcast Receiver in your Manifest.xml file. Extends broadcastreceiver class in your class that provides the onReceive() method where you will get the object of the TelephoneManager class.
 
Inside the main class, you will create another inner class that extends the PhoneStateListener class that provides onCallStateChanged that consists of a state and an incoming number as an argument. Both numbers and states will show with the help of a Toast to show the state change.
 
Step 1
 
Create the project as in the following:
  1. Click on the file
  2. click on new project
  3. click on Android application project
  4. Write the name of you application in the box depending on your project
Clipboard07.jpg
 
Step 2
 
Register a Broadcast Receiver in your Android Manifest.xml file like the following;
 
for this you will use the "receiver" tag:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.broadcastphoneste"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="17" />  
  10.    
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.broadcastphoneste.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.    
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.   <receiver  
  26.       android:name="MyReciever">  
  27.       <intent-filter>  
  28.       <action android:name="android.intent.action.PHONE_STATE"/>  
  29.       </intent-filter>  
  30.    
  31.   </receiver>  
  32.     </application>  
  33.    
  34.     <uses-permission  
  35.         android:name="android.permission.READ_PHONE_STATE"></uses-permission>  
  36. </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.
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  6.     android:paddingRight="@dimen/activity_horizontal_margin"  
  7.     android:paddingTop="@dimen/activity_vertical_margin"  
  8.     android:paddingBottom="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity">  
  10.    
  11.     <TextView  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:text="WhenEver Call comes it will show a Message" />  
  15.    
  16. </RelativeLayout> 
Step 4
 
Create a MainActivty.java class and write this:
  1. import android.os.Bundle;  
  2. import android.app.Activity;  
  3. import android.view.Menu;  
  4.    
  5. public class MainActivity extends Activity {  
  6.     @Override  
  7.     protected void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.activity_main);  
  10.     }  
  11.     @Override  
  12.     public boolean onCreateOptionsMenu(Menu menu) {  
  13.         // Inflate the menu; this adds items to the action bar if it is present.  
  14.         getMenuInflater().inflate(R.menu.main, menu);  
  15.         return true;  
  16.     }  
Step 5
 
Create another Java class that extends the BroadcastReceiver class as in the following:
  1. package com.broadcastphoneste;  
  2.    
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.telephony.PhoneStateListener;  
  7. import android.telephony.TelephonyManager;  
  8. import android.util.Log;  
  9. import android.widget.Toast;  
  10.    
  11. /** 
  12.  * Created by vivek on 10/3/13. 
  13.  */  
  14. public class MyReciever extends BroadcastReceiver {  
  15.    
  16.     Context context;  
  17.     @Override  
  18.    
  19.     public void onReceive(Context c, Intent intent) {  
  20.         context=c;  
  21.         try {  
  22.             // TELEPHONY MANAGER class object to register one listner  
  23.             TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);  
  24.             //Create Listner  
  25.             MyPhoneStateListener PhoneListener = new MyPhoneStateListener();  
  26.             // Register listener for LISTEN_CALL_STATE  
  27.             telephonyManager.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);  
  28.         } catch (Exception e) {  
  29.             Log.e("Phone Receive Error"" " + e);  
  30.         }  
  31.     }  
  32.     private class MyPhoneStateListener extends PhoneStateListener {  
  33.    
  34.         public void onCallStateChanged(int state, String incomingNumber) {  
  35.             Log.d("MyPhoneListener",state+"   incoming no:"+incomingNumber);  
  36.             if (state == 1) {  
  37.                 String msg = "New Phone Call Event. Incomming Number : "+incomingNumber;  
  38.                 Toast.makeText(context,msg,Toast.LENGTH_LONG).show();  
  39.             }  
  40.         }  
  41.     }  
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:
 
Clipboard100.jpg
 
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.
 
Step 6
 
Run your application.
 
Image 1
 
Clipboard008.jpg
 
Step 7
 
Image 2
 
Clipboard08.jpg
 
Step 8
 
Image 3
 
Clipboard09.jpg
 
Step 9
 
Image 4
 
Clipboard10.jpg


Similar Articles