0

I want to get call details(mobile number, call duration, date, time etc) of dialed mobile no after disconnecting call.

What I did so far:

I create a Broadcast Receiver to detect the call disconnect event. After getting call details I fetch the latest dialed no from call log and store in SQLite database.

What the problem is:

When I dial any no from device and disconnect that, onReceive() method called twice. Same record is inserted twice. I have checked it by printing Logs also. I searched this issue on Google and got some solution like " use sendBroadcast() only once, register broadcast receiver only once etc". But I am not calling sendBroadcast()anywhere, neither I am registering it twice.

I am new to Android so please suggest what am I doing wrong?

Broadcast Receiver:

public class CallReceiver extends BroadcastReceiver {

ContentResolver contentResolver;
Context context;
boolean is_network_roaming = false;
TelephonyManager tm = null;
AppInfo appInfo = null;
ContactHelper contactHelper;

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    String TAG = getClass().getSimpleName();
    this.context = context;
    tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    appInfo = new AppInfo(context);
    contactHelper = new ContactHelper(context);


    if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {

        is_network_roaming = tm.isNetworkRoaming();

        /* Method for getting call details from call log */
        getAllCallLogs();

    }

}

Manifest File:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.callduration"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="22" />

<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_logo"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.callduration.Splash"
        android:configChanges="orientation|keyboardHidden"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.NoTitleBar"
        android:windowSoftInputMode="adjustPan|adjustResize|stateHidden" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.callduration.MainActivity" >
    </activity>
    <activity android:name="com.callduration.CallHistory" >
    </activity>

    <receiver android:name="com.callduration.receivers.CallReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>
</application>

</manifest>
Ashish Tiwari
  • 2,168
  • 4
  • 30
  • 54
  • 1
    A dirty hack though: Put a Unique Index on your SQLite DB for your fields. SO no two are same - and if you are using a timestamp - tallyHo! But but but - this is a very dirty way of doing this. – Skynet Jan 12 '15 at 11:07
  • I am creating a Primary Key (ID) in database table and it is working fine, every record has unique ID. But I am fetching outgoing call by mobile no not by primary key. What should I do now? – Ashish Tiwari Jan 12 '15 at 11:12
  • I saw this problem in one my app with another action. I found that sometime, on some device, OS triggers two or more times BR, I used a watchdog (my var to check state) to escape multiple call. – Ivan Jan 12 '15 at 11:13
  • 1
    I am talking about a [Unique Constraint](http://stackoverflow.com/questions/2701877/sqlite-table-constraint-unique-on-multiple-columns) – Skynet Jan 12 '15 at 11:15

1 Answers1

0

The sequence of state changes when dialing a call is:

CALL_STATE_OFFHOOK--->ACTION_NEW_OUTGOING_CALL-->...{call answered or not answered}.........--> CALL_STATE_OFFHOOK-->CALL_STATE_IDLE

As per the above states, your onReceive is supposed to start twice, once when call is initiated and second when call is disconnected. To achieve what you need i.e you want to log the events of a single call session. You can use the below code,

int state=intent.getStringExtra(TelephonyManager.EXTRA_STATE)
Boolean singlecallstate=false;
switch (state) {
    case TelephonyManager.ACTION_NEW_OUTGOING_CALL:
       singlecallstate=true;
        //any other code you want
    case TelephonyManager.CALL_STATE_IDLE:
       if(singlecallstate){
          getAllCallLogs();
          singlecallstate=false;
 }
Psypher
  • 10,717
  • 12
  • 59
  • 83