• 权限:
 <!-- 打电话的状态权限 -->
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
  • 清单意图
<!--打电话的状态  -->
        <receiver android:name=".MyPhoneReceiver">
            <intent-filter >
                <action android:name="android.intent.action.PHONE_STATE"/>
            </intent-filter>
        </receiver>

结构

22 接电话状态广播_android

MainActivity.java
没有内容

MyPhoneReceiver.java

package com.qf.day22_broadcastreceiver_demo5;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;

public class MyPhoneReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        //获取电话管理者对象
        TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        //获取打电话的状态
        int state = manager.getCallState();

        //获取响铃时 电话号码
        Bundle bundle = intent.getExtras();
        String num =bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);

        switch (state) {
        case TelephonyManager.CALL_STATE_IDLE://挂断
            Log.e("AAA", "==挂断=>");
            break;
        case TelephonyManager.CALL_STATE_RINGING://响铃
            Log.e("AAA", "==响铃=>"+num);
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK://接听
            Log.e("AAA", "==接听=>");
            break;

        default:
            break;
        }

    }

}

  • 清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.qf.day22_broadcastreceiver_demo5"
    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.qf.day22_broadcastreceiver_demo5.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=".MyPhoneReceiver">
            <intent-filter >
                <action android:name="android.intent.action.PHONE_STATE"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>