How does a Android "OS" detect a incoming call
Asked Answered
U

1

8

I'd like to know:

  1. how the android OS detect a incoming call(number) and displays the contact name and gives us a option to attend the call.
  2. What happens inside the OS when the "END CALL BUTTON" is tapped.

When I searched regarding this I am getting only the Classes and methods to create my own app. Requesting for the explanation.

Unaccountedfor answered 24/11, 2014 at 12:51 Comment(0)
C
16

In Android it is possible to detect call events using the built-in TelephonyManager API.TelephonyManager class provides access to information about the telephony services on the device.

Example :

Create a new class called MyCallReceiver

package com.example;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class MyCallReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
            // This code will execute when the phone has an incoming call

            // get the phone number 
            String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
            Toast.makeText(context, "Call from:" +incomingNumber, Toast.LENGTH_LONG).show();

        } else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                TelephonyManager.EXTRA_STATE_IDLE)
                || intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                        TelephonyManager.EXTRA_STATE_OFFHOOK)) {
            // This code will execute when the call is disconnected
            Toast.makeText(context, "Detected call hangup event", Toast.LENGTH_LONG).show();

        }
    }
}

BroadcastReceiver class that will monitor the phone state and whenever there is a change in phone state, the onReceive() method of the BroadcastReceiver will be called.

Add the READ_PHONE_STATE permission in your AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.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="com.example.MyCallReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

Check this for references : BroadcastReceiver

Chloromycetin answered 24/11, 2014 at 14:25 Comment(3)
Thanks for this code. What is the difference between using BroadCast and PhoneStateListener[onCallStateChanged()]. Expecting your guidenceUnaccountedfor
Simply we can say Broadcast will work with all condition well.phonestate will work only with phone calls related things. ;-)Chloromycetin
Will onPause be called?Linhliniment

© 2022 - 2024 — McMap. All rights reserved.