Android ContentObserver onChange() 只返回 false

发布于 2024-10-07 09:32:34 字数 4886 浏览 3 评论 0原文

我正在尝试为 CallLog.Calls 内容提供程序实现 ContentObserver。我的意思是,如果我拨打电话或接听电话等,观察者必须通知我 CallLog.Calls 内容提供程序已更改。但 onchange 方法仅返回 false,即使观察者已注册并收到通知。也许我做错了什么。

这是我的代码。这是一个活动。

package com.psyhclo;


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);
            this.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);

        } 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;
    }

}
}

I'm trying to implement a ContentObserver for CallLog.Calls content provider. I mean, If I make a call, or receive a call, etc, the observer must notify me that the CallLog.Calls content provider has changed. But the onchange method only returns false, even with the observers registered, and notified. Maybe I'm doing something wrong.

This is my code. It's an Activity.

package com.psyhclo;


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);
            this.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);

        } 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;
    }

}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒江雪… 2024-10-14 09:32:34

这就是这个问题的答案。

Android onChange() 方法仅返回 false

This is the answer for this question.

Android onChange() method only returns false

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文