To calculate the talk time for incoming and outgoing calls, use the following broadcast receiver:
public class CallDurationReceiver extends BroadcastReceiver { static boolean flag = false; static long start_time, end_time; @Override public void onReceive(Context arg0, Intent intent) { String action = intent.getAction(); if (action.equalsIgnoreCase("android.intent.action.PHONE_STATE")) { if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals( TelephonyManager.EXTRA_STATE_RINGING)) { start_time = System.currentTimeMillis(); } if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals( TelephonyManager.EXTRA_STATE_IDLE)) { end_time = System.currentTimeMillis();
Register your receiver in the manifest file as follows:
<receiver android:name=".CallDurationReceiver"> <intent-filter> <action android:name="android.intent.action.PHONE_STATE" /> </intent-filter> </receiver>
Also add permission to use:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Akhil
source share