android loader用法
2016-04-21 11:19
302 查看
Loader是一个Android上的异步加载方案。
它只能实现在Activity和Fragment。
你需要实现LoaderManager.LoaderCallbacks<Cursor>。
而实现这个接口,必须实现三个虚函数:
public Loader<Cursor> onCreateLoader(int id, Bundle args);
public void onLoadFinished(Loader<Cursor> loader, Cursor data);
public void onLoaderReset(Loader<Cursor> loader);
OnCreateLoader 是在调用了getLoaderManager().initLoader(0, null, this)之后才有framework调用,并返回一个Loader<Cursor>,一般我们返回默认CursorLoader,当然我们也可以自已去实现一个AsyncTask的子类。
当CusorLoader执行完以后就调onLoadFinished()。但我们调getLoaderManager().restartLoader(0, null, this)的时候,它就会调onLoaderReset。
Android提供LoaderManager的目的就在于可以管理多个AsyncTask,注意它的ID参数。
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();
}
@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;
}
@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 = apps;
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);
}
}
@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();
}
}
@Override protected void onStopLoading()
{
//
Attempt to cancel the current load task if possible.
cancelLoad();
}
@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);
}
@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;
}
}
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.
}
}
它只能实现在Activity和Fragment。
你需要实现LoaderManager.LoaderCallbacks<Cursor>。
而实现这个接口,必须实现三个虚函数:
public Loader<Cursor> onCreateLoader(int id, Bundle args);
public void onLoadFinished(Loader<Cursor> loader, Cursor data);
public void onLoaderReset(Loader<Cursor> loader);
OnCreateLoader 是在调用了getLoaderManager().initLoader(0, null, this)之后才有framework调用,并返回一个Loader<Cursor>,一般我们返回默认CursorLoader,当然我们也可以自已去实现一个AsyncTask的子类。
当CusorLoader执行完以后就调onLoadFinished()。但我们调getLoaderManager().restartLoader(0, null, this)的时候,它就会调onLoaderReset。
Android提供LoaderManager的目的就在于可以管理多个AsyncTask,注意它的ID参数。
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();
}
@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;
}
@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 = apps;
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);
}
}
@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();
}
}
@Override protected void onStopLoading()
{
//
Attempt to cancel the current load task if possible.
cancelLoad();
}
@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);
}
@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;
}
}
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.
}
}
相关文章推荐
- 添加Android系统设置项
- Android加载图片导致内存溢出(Out of Memory异常)
- Android 菜单定制使用小结
- Android中创建对话框(确定取消对话框、单选对话框、多选对话框)实例代码
- Android内存管理机制详解
- Android版本下载以及切换
- Android项目开发实战之使用Fragment和FragmentTabHost搭建底部菜单(一)
- Android 5.0中使用JobScheduler
- Facebook的Android调试工具Stetho
- 告别编译运行 ---- Android Studio 2.0 Preview发布Instant Run功能
- Android Studio下的单元测试
- 一个android工程代码多个差异化项目管理方法探讨
- Android fragment 设置横屏后,锁屏之后重启手机导致奔溃问题
- android studio遇到的问题。
- Android自定义属性,format详解
- Android中的onWindowFocusChanged()方法详解
- 在android中使用Post方式提交数据
- Android 开发应该注意的编程规范
- Android中的Handler机制(一)
- Android获取手机应用信息