1

I am working on an android app and I need to increment the incoming missed calls.

I registered a ContentObserver. How can I check in the onChange method if the call is a missed call or not?

I have a contentobserver with the following code:

public class IncomingCall extend BroadcastReceiver
{
public void onReceive( final Context context, Intent intent)
{
String state= extras.getString(TelephonyManager.EXTRA_STATE);
    if (TelephonyManager.EXTRA_STATE_IDLE.equals(state))
        {
            context.getApplicationContext().getContentResolver().registerContentObserver(android.provider.CallLog.Calls.CONTENT_URI, true, new CallContentObserver(new Handler(), context));
        }

}

 class CallContentObserver extends ContentObserver {

    Context context;

    public CallContentObserver(Handler handler, Context context) {
        super(handler);
        this.context = context;
    }

    @Override
    public boolean deliverSelfNotifications() {
        return true;
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);

        //flag for missed calls
        how to check if last call is missed call?

Cursor c = context.getContentResolver().query(CallLog.Calls.CONTENT_URI,null,null, null, null);
        int type = c.getColumnIndex(CallLog.Calls.TYPE);
        while (c.moveToNext()) {
            String callType = c.getString(type);

            int dircode = Integer.parseInt(callType);
            switch (dircode) {

            case CallLog.Calls.MISSED_TYPE:
                flag++;

            }
            break;
        }

}

Isn't there another way to do this? (better than I did when checking the last call from CallLog database?

Kara
  • 6,115
  • 16
  • 50
  • 57
just ME
  • 1,817
  • 6
  • 32
  • 53
  • 1
    Have You tried http://stackoverflow.com/questions/7835876/how-are-call-types-incoming-outgoing-missed-stored-in-android-call-log ? – sandrstar May 16 '13 at 08:42
  • I've updated my code. This is what I've tried. IS IT OK? – just ME May 16 '13 at 08:44
  • ok, but usage of Uri from onChange(boolean selfChange, Uri uri) probably would be more effective and faster than iterating all available calls. – sandrstar May 16 '13 at 08:51
  • the code i wrote is not working correctly. the flag value is not incremeded corectly. Can you please provide me with a code? – just ME May 16 '13 at 08:56
  • what if after a missed call I have an incoming/outgoing call?:) – just ME May 16 '13 at 08:59

1 Answers1

1

I would suggest to try the following code in order to determine 'if last call is missed call or not':

@Override
public void onChange(boolean selfChange, Uri uri) {

    if (null == uri) {
        onChange(selfChange);
        return;
    }

    super.onChange(selfChange, uri);

    final Cursor c = context.getContentResolver().query(uri,null,null, null, null);
    final int type = c.getColumnIndex(CallLog.Calls.TYPE);
    final int dircode = c.getInt(type);

    switch (dircode) {
        case CallLog.Calls.MISSED_TYPE:
            flag++;
            break;
    }
}

It would be faster than checking all calls in case if You need to check only latest call. However, if where will be no uri, then provided way looks fine.

Another suggestion is to implement Service to do checking of missed calls count, so You will not have long processing in ContentObserver.

sandrstar
  • 12,503
  • 8
  • 58
  • 65
  • Throws android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 13 at line: final int dircode = c.getInt(type); – powder366 May 08 '16 at 16:28