Android的"OS"如何运行检测来电 [英] How does a Android "OS" detect a incoming call

查看:91
本文介绍了Android的"OS"如何运行检测来电的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道:

  1. android操作系统如何检测到来电(号码)并显示联系人姓名,并为我们提供了参加通话的选项.
  2. 点击"END CALL BUTTON",在操作系统内部会发生什么.

当我对此进行搜索时,我只会得到创建自己的应用程序的类和方法.要求解释.

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

推荐答案

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

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.

示例:

创建一个名为 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()方法.

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.

在您的AndroidManifest.xml中添加READ_PHONE_STATE权限

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>

检查此内容以获取参考: BroadcastReceiver

Check this for references : BroadcastReceiver

这篇关于Android的"OS"如何运行检测来电的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆