gpt4 book ai didi

Android:刷新 Activity

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

我有问题!我有 3 个 Activity 、数据库和 WebServer。假设每 20 分钟服务器清理我的数据库并用新数据填充它。每个 Activity 都使用来自数据库的数据。如何在完成新的数据库刷新后自动刷新 Activity Activity 。谢谢。

最佳答案

我建议每次更新数据库时发送广播,并在需要通知更新的每个 Activity 中注册广播接收器。

编辑示例:

在您的 DB 类中,发送此广播:

Intent intent = new Intent("my.db.updated");
context.sendBroadcast(intent);

在您需要被告知数据库更改的 Activity 中:

public void onCreate(...) {
...

BroadcastReceiver receiver = new BroadcastReciever() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction.equals("my.db.updated") {
// Do something
}
}
};

registerReceiver(receiver, new IntentFilter("my.db.updated"));
}

关于Android:刷新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5273603/

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