1. 首先创造一个空的 Activity 且删掉多余的代码, 完整的代码如下:
MainActivity.java
package com.example.test.callreceiver;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.util.Log;
public class MainActivity extends Activity {
private final String TAG="CallReceiver";
public MainActivity() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Log.i(TAG, "onCreate()");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
2. 创建 class "Receiver.java" 能接受广播, 在 onReceive() 中加入收到信息后的操作.
先获取 TelephoneManager: 可以利用 context 的 getSystemService 方法获取
(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
再创建一个 listener 用来监听及实现监听后的操作, 这个 listener extends to "PhoneStateListener"
在 onCallStateChanged() 方法中实现监听到之后的操作, 如打印电话号码及状态.
private class MyPhoneStateListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
Log.i(TAG, state + " incoming number: " + incomingNumber);
}
}
<span style="white-space:pre"> </span> // create listener
<span style="white-space:pre"> </span> MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
listener 创建之后就可以利用它来监听 “PhoneStateListener.LISTEN_CALL_STATE” 事件.
将实例化后的 PhoneListener 及要监听的事件加入到 TelephoneManager 要监听的对象及事件中.
// register listener for LISTEN_CALL_STATE
telmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
Receiver.java 完整代码如下:
package com.example.test.callreceiver;
/**
* Created by elvis on 10/13/15.
*/
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.telephony.PhoneStateListener;
public class Receiver extends BroadcastReceiver {
private final String TAG="CallReceiver";
public Receiver() {
}
private class MyPhoneStateListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
Log.i(TAG, state + " incoming number: " + incomingNumber);
}
}
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "onReceive()");
//String num = this.getResultData();
//Log.i(TAG, num);
try {
// telephone manager
TelephonyManager telmgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
// create listener
MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
// register listener for LISTEN_CALL_STATE
telmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
} catch (Exception e) {
Log.e(TAG, "Phone Receive Error " + e);
}
}
}
3. 在 AndroidManifest.xml 中加入权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test.callreceiver" >
<!-- 读取电话信息授权 -->
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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.test.callreceiver.Receiver">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
</application>
</manifest>
4. 安装 APK 之后, emulator 未来电状态
利用 logcat 来显示调试信息
adb logcat CallReceiver:I *:S
模拟电话播入的方法
此时将会在 emulator 看到
而可以看到 logcat 显示的信息
至此我们已成功的拦截播入电话的信息. ^_^