Android onChange()方法仅返回false [英] Android onChange() method only returns false

查看:135
本文介绍了Android onChange()方法仅返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a ContentObserver onChange() declared as a subclasse in my activity. But it always returns false. Can anyone tell me why?

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

(Update) This code must call the fillList if the CallLog content provider changes. I mean, if I make a new call, so the data of the call will be inserted in the content provider, so it must return to the observer that something has changed there, so it will call the fillList().But it always return false, even If I make a new call on the emulator.

这里是代码。

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

}
}


推荐答案

该函数不会返回假,因为它不会返回任何内容。其返回类型为 void 。它接收为参数。

The function doesn't return false, because it doesn't return anything. Its return type is void. It receives false as a parameter.

为什么?

好吧,我键入'android onchange'进入Google并选择第一个结果,然后发现以下内容:

Well, I typed 'android onchange' into Google and selected the first result, and found the following:

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(),则只会在此函数上记录 true输入。

So all that happened is that the cursor was changed, and not by calling its .commit() method. You will only log a 'true' input to this function if .commit() is called.

这篇关于Android onChange()方法仅返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆