这里,向大家简单介绍通过BroadcastReceiver来拦截外拨电话的方法。

1、创建PhoneReceiver继承自BroadcastReceiver

这个类是BroadcastReceiver的子类,具体的拦截操作在这个类中实现,我在这里只是简单的介绍一下方法,把获取到的外拨号码打印出来。具体的业务逻辑就要大家自己去实现了。

具体代码如下:

package com.lyz.rereiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
* 拦截拨出电话
* @author liuyazhuang
*
*/
public class PhoneReceiver extends BroadcastReceiver {

private static final String TAG = "PhoneReceiver";

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String num = getResultData();
Log.i(TAG, num);
}
}

2、在AndroidManifest.xml文件中要注册相应的授权信息

具体实现如下:

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

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<!-- 拦截外拨通话授权 -->
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.lyz.phone.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.lyz.rereiver.PhoneReceiver">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
</application>

</manifest>

大功告成,是不是很简单呢?

温馨提示:大家可以到链接下载完整的示例代码