Is it okay to Query the DownloadManager on the UI Thread?

允我心安 提交于 2019-12-10 13:08:46

问题


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?

Can calling it ever cause an ANR?


回答1:


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);
}

... this could be break down to the question whether it is safe to access cursors in the UI thread. See Mark Murphy's excellent answer to this. Extract:

So query the DownloadManager in a background thread.



来源:https://stackoverflow.com/questions/15800299/is-it-okay-to-query-the-downloadmanager-on-the-ui-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!