1.reference url
http://www.krvarma.com/posts/android/detecting-incoming-and-outgoing-calls-in-android/
2.add permission in Manifest.xml
View Code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.guc.android"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
<activity android:name=".Welcome"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Doctor"
android:label="医生信息"
android:theme="@android:style/Theme.Light" >
</activity>
<activity android:name=".HomeArchive"
android:label="家庭档案信息" >
</activity>
<receiver android:name="com.guc.comm.GucOutgoingCallReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
</application>
</manifest>
3.add class OutgongReceiver extends BroadcastReceiver
View Code
package com.guc.comm;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class GucOutgoingCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();
if (null == bundle)
return;
// String
// outgoingNumber=intent.getStringExtra(Intent.ACTION_NEW_OUTGOING_CALL);
String phoneNubmer = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i("OutgoingCallReceiver", phoneNubmer);
Log.i("OutgoingCallReceiver", bundle.toString());
//
}
}
4.save outgoing phone number when dial
View Code
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent dialIntent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:13724560911"));
startActivity(dialIntent);
// TelephonyManager tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
// PhoneStateListener phoneListener=new PhoneStateListener();
// //phoneListener.onCallStateChanged(state, incomingNumber)
// tm.listen(phoneListener, phoneListener.LISTEN_CALL_STATE);
}