应到内容解析器调用服务(即单独的线程)做什么? [英] Should calls to a Content Resolver be done in a Service (i.e separate thread)?

查看:116
本文介绍了应到内容解析器调用服务(即单独的线程)做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习更多关于Android中的内容提供商和他们的表现。例如,如果我写我自己的内容提供商(ContentProvider的子类)SQLite的操作,然后我想在一个活动的的onPause做一个操作()方法,然后在我的活动我会做:

I'm learning more about content providers in Android and they're performance. For example, if I write my own content provider (subclass ContentProvider) for SQLite operations and then I want to do an operation in an Activity's onPause() method, then in my Activity I would do:

@Override
protected void onPause (){
    super.onPause();

    // ...prepare myTransactionValues

    // This could be anything like insert, bulkInsert, query, delete, etc.
    getContentResolver().insert(CONTENT_URI, myTransactionValues)
}

不过,它看起来喜欢这个数据库操作正在主线程上完成。是工作在幕后的一个单独的线程正在做?它应该是在它自己的线程?如果应该,有没有推荐的方法(可能是一个服务)?

However, it looks likes this database operation is being done on the main thread. Is the work being done in a separate thread behind the scenes? Should it be in it's own thread? If it should, is there a recommended way (perhaps a Service)?

推荐答案

如果你想要做的事在非UI线程,你通常应该使用的 AsyncTask的。 A 服务,在另一方面,最好保持长期的任务。

If you want to do something in a Non-UI thread, you should usually use an AsyncTask. A service, on the other hand, is best kept for long tasks.

这篇关于应到内容解析器调用服务(即单独的线程)做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆