本文介绍了检测呼出和呼入的Andr​​oid挂断事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,其中我想检测两个同类的Andr​​oid相关的呼叫事件

I have a requirement wherein I want to detect two kind of events related to Calls in Android

  1. 每当传出调用时,我的应用程序应该了解这一起被叫号码
  2. 当电话挂断(由于成功/失败),我的应用程序应该知道这与挂断的原因

这是有可能在Android的?我是新来的Andr​​oid平台,因此将需要一些好详细的教程来指导我通过这个

Is this possible in Android? I am new to Android platform, hence will need some good detailed tutorials to guide me through on this

感谢

推荐答案

您应该创建一个BroadcastReceiver:

You should create a BroadcastReceiver:

public class CallReciever extends BroadcastReceiver {

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

        if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                TelephonyManager.EXTRA_STATE_RINGING)) {

                // Phone number
                String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

                // Ringing state
                // This code will execute when the phone has an incoming call
        } 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 answered or disconnected
        }

    }
}

您应该注册你的应用程序来听这些意图在清单:

You should register you application to listen to these intents in the manifest:

<receiver android:name=".CallReciever" >
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
 </receiver>

这篇关于检测呼出和呼入的Andr​​oid挂断事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 12:40
查看更多