Loaders

 

Loaders,获取数据的东西。

 

总体流程是:通过getLoaderManager().initLoader(0,null,this)获得Loader,如果没有,那么就会调用接口函数获取,注意:有ID,所以要在获取的接口函数里进行相应的选择。

 

 会自动更新数据;

一个主要依赖Activity或者Fragment存在的。

通过getLoaderManager().initLoader(0,null,this)方法初始化LOADER。

要继承LoaderManager.LoaderCallbacks,包括:

LoaderManager.LoaderCallbacks includes these methods:

  • onCreateLoader() — Instantiate and return a new Loader for the given ID.
  • onLoadFinished() — Called when a previously created loader has finished its load.
  • onLoaderReset() — Called when a previously created loader is being reset, thus making its data unavailable.

These methods are described in more detail in the following sections.

  第一个用于创建LOADER,一般来说,只有init的时候ID对应的LOADER不在的时候才会运行该方法。

  第二个一般用来获取数据后的填充适配器。

  第三个用于重置。

 

  Loader可以自定义,一般使用继承AsyncTaskLoader的类。

  

/*** A custom Loader that loads all of the installed applications.*/
public static class AppListLoader extends AsyncTaskLoader<List<AppEntry>> {final InterestingConfigChanges mLastConfig = new InterestingConfigChanges();final PackageManager mPm;List<AppEntry> mApps;PackageIntentReceiver mPackageObserver;public AppListLoader(Context context) {super(context);// Retrieve the package manager for later use; note we don't// use 'context' directly but instead the save global application// context returned by getContext().mPm = getContext().getPackageManager();}/*** This is where the bulk of our work is done.  This function is* called in a background thread and should generate a new set of* data to be published by the loader.*/@Override public List<AppEntry> loadInBackground() {// Retrieve all known applications.List<ApplicationInfo> apps = mPm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES |PackageManager.GET_DISABLED_COMPONENTS);if (apps == null) {apps = new ArrayList<ApplicationInfo>();}final Context context = getContext();// Create corresponding array of entries and load their labels.List<AppEntry> entries = new ArrayList<AppEntry>(apps.size());for (int i=0; i<apps.size(); i++) {AppEntry entry = new AppEntry(this, apps.get(i));entry.loadLabel(context);entries.add(entry);}// Sort the list.
        Collections.sort(entries, ALPHA_COMPARATOR);// Done!return entries;}/*** Called when there is new data to deliver to the client.  The* super class will take care of delivering it; the implementation* here just adds a little more logic.*/@Override public void deliverResult(List<AppEntry> apps) {if (isReset()) {// An async query came in while the loader is stopped.  We// don't need the result.if (apps != null) {onReleaseResources(apps);}}List<AppEntry> oldApps = mApps;mApps = apps;if (isStarted()) {// If the Loader is currently started, we can immediately// deliver its results.super.deliverResult(apps);}// At this point we can release the resources associated with// 'oldApps' if needed; now that the new result is delivered we// know that it is no longer in use.if (oldApps != null) {onReleaseResources(oldApps);}}/*** Handles a request to start the Loader.*/@Override protected void onStartLoading() {if (mApps != null) {// If we currently have a result available, deliver it// immediately.
            deliverResult(mApps);}// Start watching for changes in the app data.if (mPackageObserver == null) {mPackageObserver = new PackageIntentReceiver(this);}// Has something interesting in the configuration changed since we// last built the app list?boolean configChange = mLastConfig.applyNewConfig(getContext().getResources());if (takeContentChanged() || mApps == null || configChange) {// If the data has changed since the last time it was loaded// or is not currently available, start a load.
            forceLoad();}}/*** Handles a request to stop the Loader.*/@Override protected void onStopLoading() {// Attempt to cancel the current load task if possible.
        cancelLoad();}/*** Handles a request to cancel a load.*/@Override public void onCanceled(List<AppEntry> apps) {super.onCanceled(apps);// At this point we can release the resources associated with 'apps'// if needed.
        onReleaseResources(apps);}/*** Handles a request to completely reset the Loader.*/@Override protected void onReset() {super.onReset();// Ensure the loader is stopped
        onStopLoading();// At this point we can release the resources associated with 'apps'// if needed.if (mApps != null) {onReleaseResources(mApps);mApps = null;}// Stop monitoring for changes.if (mPackageObserver != null) {getContext().unregisterReceiver(mPackageObserver);mPackageObserver = null;}}/*** Helper function to take care of releasing resources associated* with an actively loaded data set.*/protected void onReleaseResources(List<AppEntry> apps) {// For a simple List<> there is nothing to do.  For something// like a Cursor, we would close it here.
    }
}

上面是官方的代码。

构造方法传入context。

loadInBackground是后台真正获取数据的代码。

deliverResult提交数据给客户端,可以直接返回。

onStartLoading处理要开始的请求,官方在这里进行了更新的监听。

onStopLoading要求停止,官方直接使用cancelLoad()。

onCanceled()取消的操作,官方建议对资源进行释放。

onReset,重置,详细看代码。

 

这样的话,就能从头到尾自定义和使用一个Loader了。

转载于:https://www.cnblogs.com/yutoulck/p/3619264.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/575542.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux内核模块开发常用内核接口

http://pan.baidu.com/s/1sjCqohv 转载于:https://www.cnblogs.com/pengdonglin137/p/3623827.html

vue 循环遍历 搜寻资料

写vue 循环遍历的 大全例子解释 转载连接 &#xff1a;https://www.cnblogs.com/xulei1992/p/6015416.html https://www.jqhtml.com/49765.html https://blog.csdn.net/qq_37591637/article/details/89072542 https://blog.csdn.net/zbw18297786698/article/details/6046673…

Qt 字符串QString arg()用法总结

原文&#xff1a;http://qimo601.iteye.com/blog/1420750 1、QString::arg()//用字符串变量参数依次替代字符串中最小数值 Cpp代码 QString i "iTest"; // current files number QString total "totalTest"; // number of files to pr…

安卓系统开机过程中logo和动画

安卓系统在开机过程中有多处会显示logo或动画&#xff0c;每个都会被next覆盖&#xff1a; boot这个是可选的&#xff0c;和硬件平台相关。boot运行过程非常短暂&#xff0c;显示logo也是一闪而过&#xff0c;所以一般不会使用。 kernel这个是内核提供的功能&#xff0c;和boot…

SVN登录时不断弹出用户名密码输入

** svn换了地址&#xff0c;所以重新定位&#xff0c;在输入账号时&#xff0c;重复报下图&#xff0c;也没有报错&#xff0c;所以就一直无效尝试。期间什么setting clear数据&#xff0c;注册表动文件&#xff0c;搜到的差不多都试了。始终一直报图一的结果。 然后最终结果…

Android Sqite数据库 6

想来想去又坑爹了,数据保存到json中去了,如何拿出来使用了,所以这一篇又要实现和上一篇相反的过程, 将Json转换成java各种对象: JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。它基于JavaScript Programming Lan…

jqgrid 列表条件查询的几步关键操作

jqgrid列表条件查询 记录一下我自己老忘的几个点 这次记录一下使用jqgrid在列表查询时的关键点&#xff0c;过一段时间不用就是忘 1】在html页面用 v-model“q.xxx” xxx是在js处定义。 2】这是js里 在jqgrid查询列表时的模块使用postData 承载参数 3】 这是在定义vue对象时…

对计算机考研的认识

发信站: 北邮人论坛 (Wed Jun 1 15:09:43 2011), 站内 不在写具体的调剂问题&#xff0c;以免大家再骂我。只客观说明一下对计算机考研的看法。供后来人参考。 首先计算机考研是统考&#xff0c;所以大家不要在各个论坛问学校有没有辅导班之类的话题。其次&#xff0c;统…

radio 取值赋值 亲测有用实效

$(“input[name‘protocolType’][checked]”).val(); 取值 $(“input[name‘radioName’][value2]”).attr(“checked”,true); 赋值 注意 广泛搜到 $(“input[name‘radioName’][valuetest]”).attr(“checked”,true); 好像不及时起到作用

wireshark: there are no interfaces on which a capture can be done

权限问题&#xff0c;简单的直接sudo就行。 更安全的做法是&#xff1a; # chmod 4755 /usr/bin/dumpcap dumpcap的所在目录可用whereis命令查看。 转载于:https://www.cnblogs.com/duanguyuan/p/3628569.html

初识:windows下的免费 ssh 客户端工具

如果你的操作系统是 Windows&#xff0c;而你想要连接 Linux 服务器相互传送文件&#xff0c;那么你需要一个简称 SSH 的 Secure Shell 软件。实际上&#xff0c;SSH 是一个网络协议&#xff0c;它允许你通过网络连接到 Linux 和 Unix 服务器。SSH 使用公钥加密来认证远程的计算…

iOS开发之通知中心(NSNotificationCenter)

前言 面向对象的设计思想是把行为方法封装到每一个对象中&#xff0c;以用来增加代码的复用性。正是这种分散封装&#xff0c;增加了对象之间的相互关联&#xff0c;总是有很多的对象需要彼此了解以及相互操作&#xff01; 一个简单示例说明这种交互产生的对象之间的相互依赖&a…

基于AT89C51单片机的8位密码锁仿真与实物制作

点击链接获取Keil源码与Project Backups仿真图&#xff1a; https://download.csdn.net/download/qq_64505944/88657969?spm1001.2014.3001.5503 源码获取 C 源码仿真图毕业设计实物制作步骤01 摘要 在日常的生活和工作中, 住宅与部门的安全防范、单位的文件档案、财务报表…

笔记本 win7 装vs2010 报错 注册表 拒绝访问等

win7-64 本子 安装vs2010过程中&#xff1a; 弹框报错提示&#xff1a;错误写入注册表键&#xff1a;HKEY-CLASS-ROOTGSC RegSetValueEx 失败&#xff1b;代码5 拒绝访问 百度经验的注册表解决办法没试。手抖点了一下忽略竟然正常安装好了。。。。mpp

PuTTY配置

目录 1、作用&#xff1f; 2、中文问题解决 &#xff1f; 3、GUI支持&#xff1f; 4、使用密钥对实现安全快捷的无密码登陆? 5、操作习惯&#xff08;AltEnter全屏以及字体配置&#xff09; 6、附录&#xff08;sshd服务器配置&#xff09; 1、作用&#xff1f;PuTTY是一个Tel…

从无到有开发连麦直播技术点整理

最近在跟老师手下的项目&#xff0c;碰到流媒体&#xff0c;流媒体服务器&#xff0c;视频编码技术&#xff0c;推流&#xff0c;拉流等概念&#xff0c;看到本篇博客整理的概念很全面&#xff0c;很自信&#xff0c;故转发留存&#xff0c;感谢原博主。 关键字 采集、前处理、…

WPF: 使用CommandManager.InvalidateRequerySuggested手动更新Command状态

WPF判断命令&#xff08;Command&#xff09;是否能够执行是通过ICommand.CanExecute事件&#xff0c;在实际程序中路由命令一般是通过CommandBinding来使命令得到实际操作代码&#xff0c;但是这个CanExecute事件的调用是由WPF控制的&#xff0c;有些时候&#xff0c;比如命令…

从开发小白到音视频专家

音视频方面的技术博客&#xff0c;转发学习 作者&#xff1a;卢俊&#xff0c;七牛云客户端团队技术负责人。拥有丰富的音视频领域的开发和实战经验&#xff0c;先后开发过 Android 播放 SDK、Android 推流 SDK、短视频 SDK&#xff0c;并主导了七牛连麦系统的设计和实现。服务…

Android如果对APK进行加密,提高反编译难度(思路)

提高反编译难度的几种方式&#xff1a; 对于软件安全来说&#xff0c;有攻就要有防才对。不然&#xff0c;Android整个产业链就会被这样的Crack给毁掉。 第一种办法&#xff1a;将核心代码用JNI写进so库中。由于so库的反编译和破解的难度加大&#xff0c;所以这种方式防止反编译…

node-media-server win环境安装架测试 踩坑记

01 因为老师的项目中需要很多流媒体模块的东西&#xff0c;接触到 Node-Media-Server 这个流媒体服务器&#xff0c;这个国人开发的组件&#xff0c;之前还有SRS也是的。记录一下问题和效果。   本子是win7&#xff0c;之前还有装nodejs时版本的问题&#xff0c;这里插一曲&a…