什么情况下可以查询在UI线程的下载管理器? [英] Is it okay to Query the DownloadManager on the UI Thread?
问题描述
的下载管理器有一个方法<一href=\"http://developer.android.com/reference/android/app/DownloadManager.html#query%28android.app.DownloadManager.Query%29\"相对=nofollow>查询()。我的问题是,如果它可以调用这个方法在UI线程,或者如果它应该只从后台线程中调用?
The DownloadManager has a method query(). My question is if it's okay to call this method on the UI Thread, or if it should only be called from a background thread?
可以在调用它曾经导致ANR?
Can calling it ever cause an ANR?
推荐答案
如果一个看起来在<一个href=\"https://github.com/android/platform_frameworks_base/blob/master/core/java/android/app/DownloadManager.java#L960\"相对=nofollow>在查询源()
方法:
If one looks at the source of the query()
method:
public Cursor query(Query query) {
Cursor underlyingCursor = query.runQuery(mResolver, UNDERLYING_COLUMNS, mBaseUri);
if (underlyingCursor == null) {
return null;
}
return new CursorTranslator(underlyingCursor, mBaseUri);
}
...这可能是打破的问题是否可以安全地在UI线程访问游标。见马克·墨菲的出色答卷这一点。摘录:
所以查询下载管理器在后台线程。
So query the DownloadManager in a background thread.
这篇关于什么情况下可以查询在UI线程的下载管理器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!