gpt4 book ai didi

Android ContentObserver 从未停止

转载 作者:行者123 更新时间:2023-11-29 18:22:43 25 4
gpt4 key购买 nike

我实现了一个 ContentObserver,它运行良好。但是现在,每当 ContentObserver 收到有关 CallLog.Calls 内容提供程序中某些更改的通知时,它都会不停地运行 onChange() 方法。

我希望我的观察者对内容提供者中发生变化的每个项目运行一次。因此,如果向 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();

}
});
}
}

最佳答案

您在 fillList() 中更改了数据库内容,所以 RatedCallsContentObserver.onChange() 被一次又一次地调用...

关于Android ContentObserver 从未停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4450778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com