开发者

Cant get latest item from calllog

I have registered an contentobserver monitoring the call log. But I cant get the latest call, the furthest I came is getting the latest call the first time, after that it just takes the previous and then the previous.. Also it returns 2 log entries at a time.

The code im using is:

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.os.Handler;
import android.provider.BaseColumns;
import android.provider.CallLog;
import android.util.Log;

class CallMonitor extends ContentObserver {

   private PositionDbAdapter mDbHelper;
   Cursor cur;
   Context CallMon = Position.MAIN_ACTIVITY;

   int idColumn;
   int numberColumn;
    int dateColumn;
    int typeColumn;
    int durColumn;

   public CallMonitor(Handler handler, Context context){
        super(handler);
        CallMon = context;
   }

   @Override
   public void onChange(boolean selfChange) {
        if(cur==null){
                cursorInit(CallMon);
        }

        if (!cur.moveToNext()) {
           //do we really want to close t开发者_开发知识库he cursor?
           //cur.close();
           return;
        }


        String number = cur.getString(numberColumn);
      String id = cur.getString(idColumn);
      String type = cur.getString(typeColumn);
      String date = cur.getString(dateColumn);
      String dur = cur.getString(durColumn);
      Date dateN = new Date(Long.parseLong(date));
      Date date2 = new Date();
      Log.d("Position", date2+": "+dateN+":"+id+", "+number+", "+type+", "+dur);
      //cur.moveToNext();
   }
   public void Destroy() {
      Log.d("Position", "Destroy Call Monitor");
      cur.close();
   }

   public void cursorInit(Context context){
        String[] projection = new String[]{
              BaseColumns._ID,
              CallLog.Calls.DATE,
              CallLog.Calls.NUMBER,
              CallLog.Calls.DURATION,
              CallLog.Calls.TYPE
        };
        ContentResolver resolver = context.getContentResolver();

        cur = resolver.query(CallLog.Calls.CONTENT_URI, projection, null,null, "date DESC");
        //cur.moveToFirst();
        numberColumn = cur.getColumnIndex(CallLog.Calls.NUMBER);
        typeColumn = cur.getColumnIndex(CallLog.Calls.TYPE);
        dateColumn = cur.getColumnIndex(CallLog.Calls.DATE);
        durColumn = cur.getColumnIndex(CallLog.Calls.DURATION);
        idColumn = cur.getColumnIndex(CallLog.Calls._ID);

   } 
}

I've tried various cur.moveToFirst .moveToNext etc...

The closest I came is when it fires the first time, i get the correct callog item, but then i get another one. Then the second time and third etc.. i get older.

I thought that i i close the cursor then it would start over from the beginning, but when I do this, it throws an error, probably because the cursor is closed when it automatically get the second one, the one i dont want.

So can somebody please help me?


I have done this type of operation but through BroadcastReceiver. may be that will be helpful for you:

public class CallReceiver extends BroadcastReceiver {


//<----------------- Fields------------->
private String phone_no = "";
private int phone_id;
private String incoming_time = "";
private Context context;


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

    context = ctx;
    final Bundle bundle = intent.getExtras();



    incomingCalls(bundle);

}


/**
 * Check incoming  call and match in database
 * @param bundle
 */
private void incomingCalls(Bundle bundle) {
    if (null == bundle)
        return;

    Log.i("IncomingCallReceiver", bundle.toString());

    final String state = bundle.getString(TelephonyManager.EXTRA_STATE);

    Log.i("IncomingCallReceiver", "State: " + state);
    phone_no = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);

    if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)
            && phone_no != null && !phone_no.equalsIgnoreCase("")) {

        final DateFormat formatter = new SimpleDateFormat(
                "MMM dd yyyy HH:mm");
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());

        incoming_time = formatter.format(calendar.getTime());
        Log.i("IncomingCallReceiver", "Incomng Number: " + phone_no);


        if (phone_id != 0) {

                Log.i("Phone number", phone_no);
                Log.i("Phone ID", phone_id+"");
                Log.i("Incoming time", incoming_time);



        }

    }
}

}


if you want call log to be displayed from older to newer entries

    ContentResolver resolver = context.getContentResolver();

    cur = resolver.query(CallLog.Calls.CONTENT_URI, projection, null,null, "date DESC");
    while(cur.moveToNext()){
    numberColumn = cur.getColumnIndex(CallLog.Calls.NUMBER);
    typeColumn = cur.getColumnIndex(CallLog.Calls.TYPE);
    dateColumn = cur.getColumnIndex(CallLog.Calls.DATE);
    durColumn = cur.getColumnIndex(CallLog.Calls.DURATION);
    idColumn = cur.getColumnIndex(CallLog.Calls._ID);

    //display call log or do ur logic
    }   

and if you want call logs to be displayed from newer to older(i.e latest entry at first)

    ContentResolver resolver = context.getContentResolver();
    cur.moveToLast()
    cur = resolver.query(CallLog.Calls.CONTENT_URI, projection, null,null, "date DESC");
    while(cur.moveToPerivous()){
    numberColumn = cur.getColumnIndex(CallLog.Calls.NUMBER);
    typeColumn = cur.getColumnIndex(CallLog.Calls.TYPE);
    dateColumn = cur.getColumnIndex(CallLog.Calls.DATE);
    durColumn = cur.getColumnIndex(CallLog.Calls.DURATION);
    idColumn = cur.getColumnIndex(CallLog.Calls._ID);

    //display call log or do ur logic
    }   
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜