Detecting outgoing calls
To be notified when an outgoing call is performed (for instance to get the callee phone number), you need to listen to theandroid.intent.action.NEW_OUTGOING_CALL Intent that will be broadcasted when an outgoing call is initiated. The received intent will have an extra string variable named Intent.EXTRA_PHONE_NUMBER which contains the outgoing number.This is how to proceed:First, you need for to add following permission to your manifest file:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />Stay at the manifest file and add the following BroadcastReceiver before </application> tag
<receiver android:name="your.package.OutgoingCallReceiver"> <intent-filter> <action android:name="android.intent.action.NEW_OUTGOING_CALL"/> </intent-filter> </receiver>Go to your OutgoingCallReceiver class and edit its onReceive method as follow:
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i(TAG, phonenumber);
Log.i(TAG, bundle.toString());
String msg = "Detected Outgoing Calls to number: " + phonenumber;
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
}
Detecting incoming calls
To be notified when an incoming call is received, you need to register a BroadcastReceiver for Intent with action
android.intent.action.PHONE_STATE that will be broadcasted when changes occur in the phone state. The received intent will have an extra string variable TelephonyManager.EXTRA_STATE which describes the phone state. If current state is TelephonyManager.EXTRA_STATE_RINGING then another extra string variable TelephonyManager.EXTRA_INCOMING_NUMBER will contain the incoming phone number.
Note if state is not
TelephonyManager.EXTRA_STATE_RINGING then no phone number can be retrived. Follow these steps: first add a permission to the manifest file: <uses-permission android:name="android.permission.READ_PHONE_STATE"/>Then add a BroadcastReceived declaration before tag:
<receiver android:name="your.package.IncomingCallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
Then go to IncomingCallReceiver class and edite onReceive method as follow:@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
Log.i(TAG, bundle.toString());
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
Log.i(TAG, "State: "+ state);
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)) {
String phonenumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.i(TAG, "Incomng Number: " + phonenumber);
String msg = "Detected Incoming Calls number: " + phonenumber;
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
}
}
Here is a good article explaining how to use SyntaxHighlighter to well format source code (e.g. Java) posted on this blog.
Aucun commentaire:
Enregistrer un commentaire