android 变量onchange,Android onChange() method only returns false

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

(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.

Here is the code.

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 callList = new ArrayList();

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

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值