您的位置:首页 > 移动开发 > Android开发

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.

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: