gpt4 book ai didi

android - ContentObserver 在一段时间后停止工作

转载 作者:行者123 更新时间:2023-11-30 02:27:08 30 4
gpt4 key购买 nike

我正在使用 ListView 编写简单的小部件。我想获得有关数据更改的即时更新,因此创建了 ContentObserver。我正在 RemoteViewFactory 子类中注册它。

@Override
public void onCreate() {
Uri uri = Uri.parse("content://mms-sms/conversations");
final String[] projection = new String[]{"*"};
_dataCursor = _context.getContentResolver().query(uri, projection, null, null, "date DESC");

if(_smsObserver == null) {
_smsObserver = new SmsObserver(new Handler(), _context);
}

_context.getContentResolver().registerContentObserver(uri, true, _smsObserver);
}

ContentObserver 在同一个类中未注册:

@Override
public void onDestroy() {
if(_dataCursor != null && !_dataCursor.isClosed())
{
_dataCursor.close();
}

if(_smsObserver != null) {
_context.getContentResolver().unregisterContentObserver(_smsObserver);
}
}

它在一段时间内工作得很好,可能几个小时,但在晚上之后,ContentObserver 停止工作并且我的 App Widget 没有得到更新。 OnChange 方法中没有高级代码。

@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Intent smsIntent = new Intent(_context, SmsWidget.class);
smsIntent.setAction(SmsWidget.RELOAD_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(_context, 0, smsIntent, PendingIntent.FLAG_UPDATE_CURRENT);
try {
pendingIntent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
}

我正在 Android 4.4.2 设备上测试我的 App Widget。

我试图将 ContentObserver 的注册和注销从 RemoteViewsFactory 移动到 AppWidgetProvider 本身到 OnEnabled()OnDisabled() 方法。不幸的是,它没有按预期工作。大约 8 小时后,ContentObserver 一定已经注销,因为我的 AppWidget 停止获取更新 Intent 。

最佳答案

我遇到过类似的问题。我的解决方案是向 AppWidgetProvider 广播自定义的 Intent 操作,因为它是接收器。

关于android - ContentObserver 在一段时间后停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27799885/

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