安卓操作系统如何检测来电?

8

我想知道:

  1. 安卓操作系统如何检测来电(电话号码),显示联系人姓名并提供接听电话的选项。
  2. 当点击“挂断”按钮时,操作系统内部会发生什么。

当我搜索相关内容时,只得到了创建自己的应用程序所需的类和方法。请求解释。

1个回答

16

在Android中,可以使用内置的TelephonyManager API来检测呼叫事件。TelephonyManager类提供了有关设备上电话服务的信息访问。

示例:

创建一个名为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类,用于监控手机状态,每当手机状态发生变化时,将调用BroadcastReceiver的onReceive()方法。

在AndroidManifest.xml文件中添加READ_PHONE_STATE权限。

<?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>

查看这个参考资料:BroadcastReceiver


谢谢你提供这段代码。使用BroadCast和PhoneStateListener[onCallStateChanged()]的区别是什么?期待您的指导。 - Pravinraj Venkatachalam
简单来说,我们可以说广播(Broadcast)在所有情况下都能正常工作。电话状态(phonestate)仅适用于与电话相关的事物。;-) - ULLAS MOHAN.V
onPause会被调用吗? - Ruchir Baronia

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接