You are on page 1of 6

Content Menu

30

C Wouldn't it be cool

Right now
the temperature in Mumbai is

if your fan could adjust to it?

KNOW MORE

Android Call State Example


We can also get the information of call state using the TelephonyManager class. For this purpose, we need to call
the listen method of TelephonyManager class by passing the PhonStateListener instance.
The PhoneStateListener interface must be implemented to get the call state. It provides one method
onCallStateChanged().

Android Call State Example


Let's see the example, where we are determining whether phone is ringing or phone is in a call or phone is neither
ringing nor in a call.

activity_main.xml
In this example, we don't have any component in this file..

Activity class
Let's write the code to know the call state.

File: MainActivity.java
1.

packagecom.javatpoint.callstates;

2.

3.

importandroid.os.Bundle;

4.

importandroid.app.Activity;

5.

importandroid.content.Context;

6.

importandroid.telephony.PhoneStateListener;

7.

importandroid.telephony.TelephonyManager;

8.

importandroid.view.Menu;

9.

importandroid.widget.Toast;

10.

11.

publicclassMainActivityextendsActivity{

12.

13.

@Override

14.

protectedvoidonCreate(BundlesavedInstanceState){

15.

super.onCreate(savedInstanceState);

16.

setContentView(R.layout.activity_main);

17.

18.

TelephonyManagertelephonyManager=

19.

(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

20.

21.

PhoneStateListenercallStateListener=newPhoneStateListener(){

22.

publicvoidonCallStateChanged(intstate,StringincomingNumber)

23.

24.

if(state==TelephonyManager.CALL_STATE_RINGING){

25.

Toast.makeText(getApplicationContext(),"PhoneIsRiging",

26.

Toast.LENGTH_LONG).show();

27.

28.

if(state==TelephonyManager.CALL_STATE_OFFHOOK){

29.

Toast.makeText(getApplicationContext(),"PhoneisCurrentlyinAcall",

30.

Toast.LENGTH_LONG).show();

Right now
the
temperature in
Mumbai is

30

Wouldn't it
be cool
if your fan
could adjust
to it?
KNOW MORE

31.

32.

33.

if(state==TelephonyManager.CALL_STATE_IDLE){

34.

Toast.makeText(getApplicationContext(),"phoneisneitherringingnorinacall",

35.

Toast.LENGTH_LONG).show();

36.

37.

38.

};

39.

telephonyManager.listen(callStateListener,PhoneStateListener.LISTEN_CALL_STATE);

40.

41.

42.

43.

@Override

44.

publicbooleanonCreateOptionsMenu(Menumenu){

45.

//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.

46.

getMenuInflater().inflate(R.menu.main,menu);

47.

returntrue;

48.

49.

50.

AndroidManifest.xml
You need to provide READ_PHONE_STATE permission in the AndroidManifest.xml file.

File: AndroidManifest.xml
1.

<?xmlversion="1.0"encoding="utf-8"?>

2.

<manifestxmlns:androclass="http://schemas.android.com/apk/res/android"

3.

package="com.javatpoint.callstates"

4.

android:versionCode="1"

5.

android:versionName="1.0">

6.

7.

<uses-sdk

8.

android:minSdkVersion="8"

9.

android:targetSdkVersion="17"/>

10.

11.

12.

<uses-permissionandroid:name="android.permission.READ_PHONE_STATE"/>

13.

14.

<application

15.

android:allowBackup="true"

16.

android:icon="@drawable/ic_launcher"

17.

android:label="@string/app_name"

18.

android:theme="@style/AppTheme">

19.

<activity

20.

android:name="com.javatpoint.callstates.MainActivity"

21.

android:label="@string/app_name">

22.

<intent-filter>

23.

<actionandroid:name="android.intent.action.MAIN"/>

24.

25.

<categoryandroid:name="android.intent.category.LAUNCHER"/>

26.

</intent-filter>

27.

</activity>

28.

</application>

29.

30.

</manifest>

download this android example


Output:

<<prev

next>>

You might also like