I have a data loading system set up using a custom Loader and Cursor that is working great from Activities and Fragments but there is no LoaderManager (that I can find) in Service. Does anyone know why LoaderManager was excluded from Service? If not is there a way around this?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
As stated in the other answer,
LoaderManager
was explicitly designed to manageLoaders
through the lifecycles ofAcivities
andFragments
. SinceServices
do not have these configuration changes to deal with, using aLoaderManager
isn't necessary.Yes, the trick is you don't need to use a
LoaderManager
, you can just work with yourLoader
directly, which will handle asynchronously loading your data and monitoring any underlying data changes for you, which is much better than querying your data manually.First, create, register, and start loading your
Loader
when yourService
is created.Next, implement
OnLoadCompleteListener<Cursor>
in yourService
to handle load callbacks.Lastly, don't forget clean up your
Loader
when theService
is destroyed.Unfortunately, no. Loaders were designed for activities and fragments in order to cleanly handle configuration changes that occur in Activites and Fragments. i.e. Rotating your device and re-attaching to the existing data.
A service does not have any configuration changes, it will sit in the background until it completes or the system is forced to kill it. So assuming you're executing your code on a background thread in your Service (which you should be anyways), theres just no reason to use a Loader. Simply make the calls you need to query your data.
So if your Service is just an IntentService, you can write your logic to query your cursor-backed data in the onHandleIntent() method.
http://developer.android.com/guide/components/loaders.html