1

私は ContentObserver を実装しましたが、うまくいきました。しかし、ContentObserver は、CallLog.Calls コンテンツ プロバイダーの変更について通知されるたびに、onChange() メソッドを停止せずに実行します。

コンテンツ プロバイダーで変更されたアイテムごとにオブザーバーを 1 回実行したいと考えています。したがって、CallLog.Calls コンテンツ プロバイダに 5 つの新しいアイテムが追加された場合、オブザーバに 5 回通知する必要があり、オブザーバに通知するたびに、onChange() メソッドへの新しい呼び出しが発生する必要があります。

これが私のコードです。

public class RatedCalls extends ListActivity {

private static final String LOG_TAG = "RATEDCALLSOBSERVER";
private Handler handler = new Handler();
private RatedCallsContentObserver callsObserver = null;
private SQLiteDatabase db;
private Cursor cursor;
StringBuilder sb = new StringBuilder();
OpenHelper openHelper = new OpenHelper(RatedCalls.this);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getApplicationContext()
            .getContentResolver()
            .registerContentObserver(
                    android.provider.CallLog.Calls.CONTENT_URI, true,
                    new RatedCallsContentObserver(handler));
    Log.d("FILLLIST", "calling from onCreate()");
}

class RatedCallsContentObserver extends ContentObserver {
    public RatedCallsContentObserver(Handler h) {
        super(h);
    }

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

    @Override
    public void onChange(boolean selfChange) {
        Log.d(LOG_TAG, "RatedCallsContentObserver.onChange( " + selfChange
                + ")");
        super.onChange(selfChange);
        fillList();
    }
}

private void fillList() {

    cursor = getContentResolver().query(
            android.provider.CallLog.Calls.CONTENT_URI, null, null, null,
            android.provider.CallLog.Calls.DATE + " DESC ");
    Log.d("FILLLIST", "Calling from filllist");

    db = openHelper.getWritableDatabase();

    startManagingCursor(cursor);
    int numberColumnId = cursor
            .getColumnIndex(android.provider.CallLog.Calls.NUMBER);
    int durationId = cursor
            .getColumnIndex(android.provider.CallLog.Calls.DURATION);
    int contactNameId = cursor
            .getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME);
    int dateId = cursor.getColumnIndex(android.provider.CallLog.Calls.DATE);
    int numTypeId = cursor
            .getColumnIndex(android.provider.CallLog.Calls.CACHED_NUMBER_TYPE);

    Date dt = new Date();
    int hours = dt.getHours();
    int minutes = dt.getMinutes();
    int seconds = dt.getSeconds();
    String currTime = hours + ":" + minutes + ":" + seconds;

    ArrayList<String> callList = new ArrayList<String>();
    cursor.moveToFirst();

    String contactNumber = cursor.getString(numberColumnId);
    String contactName = cursor.getString(contactNameId);
    String duration = cursor.getString(durationId);
    String callDate = DateFormat.getDateInstance().format(dateId);
    String numType = cursor.getString(numTypeId);

    ContentValues values = new ContentValues();

    values.put("contact_id", 1);
    values.put("contact_name", contactName);
    values.put("number_type", numType);
    values.put("contact_number", contactNumber);
    values.put("duration", duration);
    values.put("date", callDate);
    values.put("current_time", currTime);
    values.put("cont", 1);
    getBaseContext().getContentResolver().notifyChange(
            android.provider.CallLog.Calls.CONTENT_URI,
            new RatedCallsContentObserver(handler));
    db.insert(CallDataHelper.TABLE_NAME, null, values);
    Toast.makeText(getBaseContext(), "Inserted!", Toast.LENGTH_LONG);
    callList.add("Contact Number: " + contactNumber + "\nContact Name: "
            + contactName + "\nDuration: " + duration + "\nDate: "
            + callDate);

    setListAdapter(new ArrayAdapter<String>(this, R.layout.listitem,
            callList));
    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

    lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Toast.makeText(getApplicationContext(),
                    ((TextView) view).getText(), Toast.LENGTH_SHORT).show();

        }
    });
}   
}
4

2 に答える 2

2

内のDBコンテンツを変更するfillList()ためRatedCallsContentObserver.onChange()、何度も呼び出されます...

于 2010-12-16T22:09:10.403 に答える
1

このクエリに日付チェックを含めます.. android.provider.CallLog.Calls.DATE > last date

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

これがあなたを助けることを願っています

于 2010-12-16T16:10:47.327 に答える