gpt4 book ai didi

android - 线程 block ContentObserver

转载 作者:行者123 更新时间:2023-11-30 03:37:04 28 4
gpt4 key购买 nike

我目前正在开发的应用程序必须对日历或事件数据库中的更改使用react,因此下面的 Observer 将注册到 URI:

content://com.android.calendar (for older Devices: content://calendar)

“问题”是当相应的数据发生变化时,观察者(有时)会被调用多次。如果我注册两个单独的 ContentResolver,一个用于 .../calendars,另一个用于 .../events,它们仍然经常被调用多次。我尝试使用以下代码实现的是缓冲这些多次调用,因为 ContentResolver 本身调用一个服务,这将运行大量代码。因此,对于短时间内的许多 ContentObserver 调用,服务应该只调用一次。

public class Observer extends ContentObserver{

private Context con;

public Observer(Handler handler, Context con) {
super(handler);
this.con = con;
}

@Override
public void onChange(boolean selfChange) {
Log.i("TS", "Änderung an den Kalendern");

//Gets released after the first Change, waits and checks SharedPrefs in order to buffer multiple Calls in a short period of time!
//Changes get handled in the Service
Thread buffer = new Thread(){

@Override
public void run() {
int check = 1, last = 0;

do{
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}

last = check;
check = getCurrent();
} while(last != check);

releaseIntent();
}

};

SharedPreferences prefs = con.getSharedPreferences(con.getPackageName(), Context.MODE_PRIVATE);
Editor edit = prefs.edit();

int first = prefs.getInt(Constants.FIRST_ON_CHANGE, 1);

if(first == 1)
buffer.run();

first++;

edit.putInt(Constants.FIRST_ON_CHANGE, first);
edit.commit();

}

//returns the current control-integer from SharedPrefs (for Thread)
private int getCurrent(){
SharedPreferences prefs = con.getSharedPreferences(con.getPackageName(), Context.MODE_PRIVATE);
return prefs.getInt(Constants.FIRST_ON_CHANGE, 1);
}

//releases ContentChanged-Intent for Service, resets SharedPrefs
private void releaseIntent(){
con.getSharedPreferences(con.getPackageName(), Context.MODE_PRIVATE).edit().putInt(Constants.FIRST_ON_CHANGE, 1).commit();


AlarmManager alm = (AlarmManager) con.getSystemService(Context.ALARM_SERVICE);
alm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), new PendingIntentCreator(con).createContentChangedIntent());
}
}

我解决这个问题的想法是,如果保存的值(来自 SharedPreferences 的“int first”)等于 1,则生成一个线程。如果观察者在该线程休眠时再次调用,则该值将被提升并且线程会再次休眠...

不幸的是,Thread 阻塞了其他传入调用,因此它的循环永远不会被扩展。我原始代码中的日志向我展示了线程完成后 SharedPreferences 发生了变化!

  • 有人能为我提供解决方案吗? (我是线程新手...)
  • 我应该为这个缓冲工作实现另一个服务吗?
  • 一般:可以将 Context 传输到 ContentObserver 吗?

提前致谢! ;)

最佳答案

我发现了我犯的错误:

我不得不通过 buffer.start() 启动线程,而不是调用 buffer.run(),因为调用 .run() 只是执行实现运行方法...

In other words, the client code instantiating a thread shouldn't place a call to the run() method on the newly instantiated thread.Because the very fact that calling the run() method on a thread object would immediately execute the steps in the run() method defeats the very purpose of multithreaded programming which is essentially of a non-deterministic nature in terms of order of execution of the concurrently running threads.

http://www.coderanch.com/t/234040/threads/java/Difference-run-start-method-Thread

关于android - 线程 block ContentObserver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16502920/

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