Cursor Loader Data not getting refreshed

45 views Asked by At

I am using a CursorLoader with ContentProvider to fetch the data into a cursor. As far as I know, the cursor loader registers a Content Observer, so whenever the underlying data changes for the cursor, the loader's onLoadFinished should be called.

In my code, a new row is inserted onto content://com.example.phonehistory/call uri, whenever a call state changes( like when a call is in INCOMING and then it is cut(EXTRA_STATE_IDLE in BroadCastReciever)). I insert the data whenever I get this state change.

The problem is that the loader's onLoadFinished is not getting called when a new row is inserted.

Pls note that my application is running when I got this issue.

Loader Callbacks

@Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        Toast.makeText(this, "onCreate for loader", Toast.LENGTH_SHORT).show();

        Uri uri1 = Uri.parse("content://com.example.phonehistory/call");
         CursorLoader loader = new CursorLoader(
                 this,
                 uri1,
                 null,
                 null,
                 null,
                 null);
         return loader;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {

        Toast.makeText(this, "onload finished", Toast.LENGTH_SHORT).show();
        if(loader.getId()==1){
            showDataChanges(cursor);
        }
    }

Content Provider's insert and query method:

@Override
    public Uri insert(Uri uri, ContentValues values) {
        Uri insertUri;
        int match = myUriMatcher.match(uri);
        System.out.println("Insert");

        SQLiteDatabase db = mDatabaseHelpwe.getWritableDatabase();

        switch(match){
            case CALL :db.insert("call_history", null, values);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return null;
    }

    @Override
    public boolean onCreate() {
        // TODO Auto-generated method stub
        mDatabaseHelpwe = new DBHelper(getContext());
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        System.out.println("Query");

        SQLiteDatabase db = mDatabaseHelpwe.getWritableDatabase();
        Cursor c=null;
        int match = myUriMatcher.match(uri);
        switch(match){
            case CALL :c = db.query("call_history", projection, selection, selectionArgs, null, null, null);
        }
        c.setNotificationUri(getContext().getContentResolver(), uri);
        return c;

    }

BroadCastReciever to insert data:

@Override
public void onReceive(Context c, Intent i) {
    // TODO Auto-generated method stub
    Bundle bundle=i.getExtras();

    if(bundle==null)
        return;

    SharedPreferences sp=c.getSharedPreferences("ZnSoftech", Activity.MODE_PRIVATE);

    String s=bundle.getString(TelephonyManager.EXTRA_STATE);

    if(i.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL))
    {
        String number=i.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        sp.edit().putString("number", number).commit();
        sp.edit().putString("state", s).commit();
    }

    else if(s.equals(TelephonyManager.EXTRA_STATE_RINGING))
    {
        String number=bundle.getString("incoming_number");
        sp.edit().putString("number", number).commit();
        sp.edit().putString("state", s).commit();
    }

    else if(s.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
    {
        sp.edit().putString("state", s).commit();
    }

    else if(s.equals(TelephonyManager.EXTRA_STATE_IDLE))
    {
        String state=sp.getString("state", null);
        if(!state.equals(TelephonyManager.EXTRA_STATE_IDLE))
        {
            sp.edit().putString("state", null).commit();
        }
        sp.edit().putString("state", s).commit();
        getCalldetailsNow(c);

    }
}

private void getCalldetailsNow(Context context) {
    // TODO Auto-generated method stub

    Cursor managedCursor = context.getContentResolver().query(
            CallLog.Calls.CONTENT_URI, null, null, null,
            android.provider.CallLog.Calls.DATE + " DESC");

    int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
    int duration1 = managedCursor
            .getColumnIndex(CallLog.Calls.DURATION);
    int type1 = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
    int date1 = managedCursor.getColumnIndex(CallLog.Calls.DATE);

    if (managedCursor.moveToFirst() == true) {
        String phNumber = managedCursor.getString(number);
        String callDuration = managedCursor.getString(duration1);

        String type = managedCursor.getString(type1);
        String date = managedCursor.getString(date1);

        String dir = null;
        int dircode = Integer.parseInt(type);
        switch (dircode) {
        case CallLog.Calls.OUTGOING_TYPE:
            dir = "OUTGOING";
            break;
        case CallLog.Calls.INCOMING_TYPE:
            dir = "INCOMING";
            break;
        case CallLog.Calls.MISSED_TYPE:
            dir = "MISSED";
            break;
        default:
            dir = "MISSED";
            break;
        }

        SimpleDateFormat sdf_date = new SimpleDateFormat("dd/MM/yyyy");
        SimpleDateFormat sdf_time = new SimpleDateFormat("h:mm a");
        // SimpleDateFormat sdf_dur = new SimpleDateFormat("KK:mm:ss");

        String dateString = sdf_date.format(new Date(Long
                .parseLong(date)));
        String timeString = sdf_time.format(new Date(Long
                .parseLong(date)));
        // String duration_new=sdf_dur.format(new
        // Date(Long.parseLong(callDuration)));
        ContentValues values = new ContentValues();

        values.put("number", phNumber);
        values.put("date", dateString);
        values.put("time", timeString);
        values.put("duration", callDuration);
        values.put("type", dir);

        Uri uri1 = Uri.parse("content://com.example.phonehistory/call");

        context.getContentResolver().insert(uri1, values);
    }

    managedCursor.close();
}

I have spend hours but not able to figure out what problem is.

0

There are 0 answers