1

我有一个 ContentObserver onChange() 在我的活动中声明为子类。但它总是返回 false。谁能告诉我为什么?

(更新) 如果 CallLog 内容提供程序发生更改,此代码必须调用 fillList。我的意思是,如果我进行一个新的调用,那么调用的数据将被插入到内容提供者中,所以它必须向观察者返回那里发生了一些变化,所以它会调用 fillList()。但它总是返回假的,即使我在模拟器上打了一个新电话。

这是代码。

    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 CallDataHelper dh = null;
StringBuilder sb = new StringBuilder();
OpenHelper openHelper = new OpenHelper(RatedCalls.this);

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

    public void onChange(boolean selfChange) {
        Log.d(LOG_TAG, "RatedCallsContentObserver.onChange( " + selfChange
                + ")");

    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    registerContentObservers();
    fillList();
}

@Override
public void onStart() {

    super.onStart();
    registerContentObservers();

}

@Override
public void onStop() {

    super.onStop();
    unregisterContentObservers();

}

private void fillList() {

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

    cursor.setNotificationUri(getBaseContext().getContentResolver(),
            android.provider.CallLog.Calls.CONTENT_URI);

    dh = new CallDataHelper(this);
    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);
    // int contactIdColumnId =
    // cursor.getColumnIndex(android.provider.ContactsContract.RawContacts.CONTACT_ID);

    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>();
    if (cursor.moveToFirst()) {

        do {
            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, null);

            callList.add("Contact Number: " + contactNumber
                    + "\nContact Name: " + contactName + "\nDuration: "
                    + duration + "\nDate: " + callDate);
            this.db.insert(CallDataHelper.TABLE_NAME, null, values);
            Toast.makeText(getBaseContext(), "Inserted!", Toast.LENGTH_LONG);

        } while (cursor.moveToNext());
        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();

            }
        });
    }
}

private void registerContentObservers() {
    ContentResolver cr = getContentResolver();
    callsObserver = new RatedCallsContentObserver(handler);
    cr.registerContentObserver(android.provider.CallLog.Calls.CONTENT_URI,
            true, callsObserver);
}

private void unregisterContentObservers() {

    ContentResolver cr = getContentResolver();
    if (callsObserver != null) { // just paranoia
        cr.unregisterContentObserver(callsObserver);
        callsObserver = null;
    }

}
}
4

1 回答 1

2

该函数不返回false,因为它不返回任何东西。它的返回类型是void. 它接收false 作为参数。

为什么?

好吧,我在 Google 中输入了 'android onchange' 并选择了第一个结果,结果如下:

This method is called when a change occurs to the cursor that is being observed.
Parameters
selfChange    true if the update was caused by a call to commit on the cursor
              that is being observed. 

所以所发生的只是光标被改变了,而不是通过调用它的.commit()方法。.commit()如果被调用,您只会将“真实”输入记录到此函数。

于 2010-12-13T17:48:35.113 回答