1

I want to save the outgoing call number and duration using broadcastreceiver service in android. I used the below code to achieve the functionality but it throws error.

public class OutgoingReceiver extends BroadcastReceiver {
    public OutgoingReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        try
        {
            Bundle bundle = intent.getExtras();
            number = bundle.getString(Intent.EXTRA_PHONE_NUMBER);
            dbOutgoing = new DBOutgoing(ctx);            
            dbOutgoing.InsertOutGoingCallDB(number, "0", "0");
            Toast.makeText(ctx, 
                "Outgoing: "+number, 
                Toast.LENGTH_LONG).show();
        }
        catch(FileNotFoundException e)
        {
            e.printStackTrace();
            Toast.makeText(ctx, String.valueOf(e),Toast.LENGTH_LONG).show();
        }  
    }
}

The above code is giving the outgoing call number, but I need the duration also after the call ends.

inf3rno
  • 24,976
  • 11
  • 115
  • 197

1 Answers1

0

From : Get Last Call Duration in android and Intent to be fired when a call ends?

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.gopi"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <receiver android:name=".IncomingCallTracker">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>

    </application>
</manifest> 



public class IncomingCallTracker extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle bundle = intent.getExtras();

            Set<String> keys = bundle.keySet();
        for (String key : keys) {
                Log.i("MYAPP##", key + "="+ bundle.getString(key));
        }       
    }

}

You can look for the key 'state' in the bundle. When its value is 'IDLE' it means call has ended and you can perform whatever action you want to based on this.

If state is 'IDLE'

 Uri contacts = CallLog.Calls.CONTENT_URI;
        Cursor managedCursor = mContext.getContentResolver().query(
                contacts, null, null, null, null);
        int number = managedCursor.getColumnIndex( CallLog.Calls.NUMBER ); 
        int duration1 = managedCursor.getColumnIndex( CallLog.Calls.DURATION);
        // movetoFirst() gives last ended call
        if( managedCursor.moveToFirst() == true ) {
            String phNumber = managedCursor.getString( number );
            String callDuration = managedCursor.getString( duration1 );
        }
        managedCursor.close();
Community
  • 1
  • 1
Shivam Verma
  • 7,973
  • 3
  • 26
  • 34
  • thanks for your code but how can i find the state of the bundle. I tried bundle.getString(TelephonyManager.EXTRA_STATE) but i dont know whether its correct? – user3490327 May 03 '14 at 18:49
  • And i need it for outgoing calls. Any one share your ideas. – user3490327 May 03 '14 at 18:51
  • Broadcast Receiver for outgoing calls : http://stackoverflow.com/questions/9569118/how-do-you-receive-outgoing-call-in-broadcastreceiver – Shivam Verma May 03 '14 at 19:26
  • If you execute the above code, you'll see the list of keys available. Here : Log.i("MYAPP##", key + "="+ bundle.getString(key)); – Shivam Verma May 03 '14 at 19:39