-
Listen outgoing call
Register a broadcast receiver with action android.intent.action.NEW_OUTGOING_CALL,
but please request to use permission android.permission.PROCESS_OUTGOING_CALLS. You can get outgoing phone number with following statement in method BroadcastReceiver.onReceive(xxx);
String strPhoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); - Listen incoming call
Use TelephonyManager and PhoneStateListener
import android.app.Activity;
import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
public class Telephony extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TelephonyManager mTelephonyMgr =
(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
mTelephonyMgr.listen(new TeleListener(),
PhoneStateListener.LISTEN_CALL_STATE);
setContentView(xxxxxxxx);
}
class TeleListener extends PhoneStateListener
{
public void onCallStateChanged(int state, String incomingNumber)
{
super.onCallStateChanged(state, incomingNumber);
switch (state)
{
case TelephonyManager.CALL_STATE_IDLE: //The call is terminated
//CALL_STATE_IDLE;
break;
case TelephonyManager.CALL_STATE_OFFHOOK: //The call is answered
//CALL_STATE_OFFHOOK;
break;
case TelephonyManager.CALL_STATE_RINGING: //The new call is comming
//CALL_STATE_RINGING
break;
default:
break;
}
}
}
} - Implement Call Filter/Firewall
- Please refer to:
%20package:%5C.googlecode%5C.com&d=0 - Sample:
- Prepare ITelephony.aidl
Download iTelephony.aidl from
then
add that file to src/com/android/internal/telephony/ of your project
- Add
permissions to AndroidManifest.xml
- Write Code
import java.lang.reflect.Method;
import com.android.internal.telephony.ITelephony;
import android.telephony.*;
......
private TelephonyManager
m_telephonyManager;
private ITelephony
m_telephonyService;
private PhoneStateListener m_phoneStateListener;
private
AudioManager m_audioManager;
....
m_telephonyManager =
(TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Class
c = Class.forName(m_telephonyManager.getClass().getName());
Method m
= c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
m_telephonyService
= (ITelephony)m.invoke(m_telephonyManager);
//You can also replace above 4 lines of statements to
//m_telephonyService
= ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELE
m_audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
m_telephonyManager.listen(new
MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
....
class
MyPhoneStateListener extends PhoneStateListener
{
public void
onCallStateChanged(int state, String incomingNumber)
{
m_audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
switch (state)
{
case
TelephonyManager.CALL_STATE_IDLE:
m_audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
break;
case
TelephonyManager.CALL_STATE_OFFHOOK:
//CALL_STATE_OFFHOOK;
break;
case
TelephonyManager.CALL_STATE_RINGING:
if (xx) // if
incomingNumber need to be blocked
{
m_telephonyService.endCall();
}
else
{
m_audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
break;
default:
break;
}
}
}
} - xxxxx
- xxxx
- Replace Incoming call screen
Sample: Android Caller ID
- xxx
阅读(4798) | 评论(0) | 转发(0) |