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:
-
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) {
-
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
Step 5
Create another Java class that extends the BroadcastReceiver class as in the following:
- 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;
-
-
-
-
- public class MyReciever extends BroadcastReceiver {
-
- Context context;
- @Override
-
- public void onReceive(Context c, Intent intent) {
- context=c;
- try {
-
- TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
-
- MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
-
- 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();
- }
- }
- }
- }
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.
Step 6
Run your application.
Image 1
Step 7
Image 2
Step 8
Image 3
Step 9