package android.content;
import android.database.ContentObserver;
import android.os.Handler;
import android.util.DebugUtils;
import java.io.FileDescriptor;
import java.io.PrintWriter;
/**
* 该类并没有做任何实质性的操作,只做了以下几件事情
* 一是提供一系列生命周期方法,start,cancel,stop,reset等
* 并将真正的操作用onStart,onCancel,onStop等形式以模板的形式提供出来由子类去实现
* 二是:提供回调接口,数据加载完成,加载取消的回调接口以及接口的注册和注销
* A class that performs asynchronous loading of data. While Loaders are active
* they should monitor the source of their data and deliver new results when the
* contents change.
*/
public class Loader {
int mId;
OnLoadCompleteListener mListener;
OnLoadCanceledListener mOnLoadCanceledListener;
Context mContext;
boolean mStarted = false;
boolean mAbandoned = false;
boolean mReset = true;
boolean mContentChanged = false;
boolean mProcessingChange = false;
/**
* 内容观察者,在数据发生变化的时候调用onContentChanged(),
* onContentChanged()方法根据started标志位来决定是执行forceLoad还是将mCotentChanged设true
*/
public final class ForceLoadContentObserver extends ContentObserver {
public ForceLoadContentObserver() {
super(new Handler());
}
@Override
public boolean deliverSelfNotifications() {
return true;
}
@Override
public void onChange(boolean selfChange) {
onContentChanged();
}
}
/**
* loader加载完数据时使用的回调接口
*/
public interface OnLoadCompleteListener {
public void onLoadComplete(Loader loader, D data);
}
/**
* loader取消使用的回调接口
*/
public interface OnLoadCanceledListener {
public void onLoadCanceled(Loader loader);
}
//无论传递的context是什么都会转化成ApplicationContext
public Loader(Context context) {
mContext = context.getApplicationContext();
}
/**
* 调用deliverResult执行listener的onLoadComplete回调方法
* 把loader加载的数据用mListener传递出去
*/
public void deliverResult(D data) {
if (mListener != null) {
mListener.onLoadComplete(this, data);
}
}
/**
* 调用此方法告知mOnLoadCanceledListener,loader已经cancel
*/
public void deliverCancellation() {
if (mOnLoadCanceledListener != null) {
mOnLoadCanceledListener.onLoadCanceled(this);
}
}
/**
* 注册回调 ,也就是给OnLoadCompleteListener mListener赋值
*/
public void registerListener(int id, OnLoadCompleteListener listener) {
if (mListener != null) {
throw new IllegalStateException("There is already a listener registered");
}
mListener = listener;
mId = id;
}
/**
* 注销OnLoadCompleteListener mListener,也就是将它置为空
* Remove a listener that was previously added with
* Must be called from the process's main thread.
*/
public void unregisterListener(OnLoadCompleteListener listener) {
if (mListener == null) {
throw new IllegalStateException("No listener register");
}
if (mListener != listener) {
throw new IllegalArgumentException("Attempting to unregister the wrong listener");
}
mListener = null;
}
/**
* 同上
*/
public void registerOnLoadCanceledListener(
OnLoadCanceledListener listener) {
if (mOnLoadCanceledListener != null) {
throw new IllegalStateException(
"There is already a listener registered");
}
mOnLoadCanceledListener = listener;
}
/**
* 同上
*/
public void unregisterOnLoadCanceledListener(
OnLoadCanceledListener listener) {
if (mOnLoadCanceledListener == null) {
throw new IllegalStateException("No listener register");
}
if (mOnLoadCanceledListener != listener) {
throw new IllegalArgumentException(
"Attempting to unregister the wrong listener");
}
mOnLoadCanceledListener = null;
}
/**
* 该方法会由LoaderManager自动执行,在使用loadermanager的时候就不要自己再去执行该方法
* 改变标志位,并执行 onStartLoading方法,所以onStartLoading方法是在loadermanager调用
* startLoading之后被执行的
*/
public final void startLoading() {
mStarted = true;//mStarted为true
mReset = false;
mAbandoned = false;
onStartLoading();
}
/**
* 子类需要实现这个方法去加载自己的数据,这个方法是在startLoading中被调用的,不由客户端自己调用
*/
protected void onStartLoading() {
}
/**
* Attempt to cancel the current load task. Must be called on the main
* thread of the process.
* 尝试取消一个loader操作,取消不是一个立刻会执行的操作,应为load是在子线程中执行
* 如果task不能被cancel或者startLoading还没被调用的话返回false
* 否则返回true,当返回true的时候,任务还在执行并指任务完成的时候OnLoadCanceledListener会被调用
*/
public boolean cancelLoad() {
return onCancelLoad();
}
/**
* 这里是一个默认实现
* 子类需要实现这个方法
* 如果任务已经完成或者任务还没开始则返回false,否则返回true
* 当返回true的时候,任务正在执行,当任务执行完成,回调OnLoadCanceledListener
*/
protected boolean onCancelLoad() {
return false;
}
/**强制开始加载数据,和startLoading()不同的是,该方法会忽略之前加载的数据集并去加载新的数据
* 该方法会调用onForceLoad方法
* Must be called from the process's main thread.
*/
public void forceLoad() {
onForceLoad();
}
/**
* 子类需要实现这个方法,这里是一个空实现
*/
protected void onForceLoad() {
}
/**
*
* 该方法在fragment/activity停止的时候由loadermanger自动去执行
* 将mStarted置为false并调用onStopLoading方法
* Stops delivery of updates until the next time startLoading() is
* called. Implementations should not invalidate their data at this
* point -- clients are still free to use the last data the loader reported.
* They will, however, typically stop reporting new data if the data
* changes; they can still monitor for changes, but must not report them to
* the client until and if startLoading() is later called.
*/
public void stopLoading() {
mStarted = false;
onStopLoading();
}
/**
* 在stopLoading执行时调用该方法
* 子类需要实现这个方法
*/
protected void onStopLoading() {
}
/**
* 在loadermanger重启一个loadre时使用
* Tell the Loader that it is being abandoned. This is called prior to
* reset to have it retain its current data but not report any new
* data.
*/
public void abandon() {
mAbandoned = true;
onAbandon();
}
/**
*子类需要实现该方法
*
*/
protected void onAbandon() {
}
/**
* 销毁loader时由loadrmanger自动调用
* 重置loader,此时loader需要释放资源
* 各个标志位设置为false ,mReset为true
*/
public void reset() {
onReset();
mReset = true;
mStarted = false;
mAbandoned = false;
mContentChanged = false;
mProcessingChange = false;
}
/**
*
*/
protected void onReset() {
}
/**
* Take the current flag indicating whether the loader's content had changed
* while it was stopped. If it had, true is returned and the flag is
* cleared.
*/
public boolean takeContentChanged() {
//mContentChanged是在onContentChanged方法中started为false设true
boolean res = mContentChanged;
mContentChanged = false;
mProcessingChange |= res;
return res;
}
/**
*/
public void commitContentChanged() {
mProcessingChange = false;
}
/**
*
*/
public void rollbackContentChanged() {
if (mProcessingChange) {
onContentChanged();
}
}
//ForceLoadContentObserver的onChange方法里调用此方法
//也就是说在数据发生改变的时候内容观察者的onChange方法被回调,会调用此方法来重新加载数据
//首先会检查当前loader是否started如果是就执行forceLoad,如果不是就设置一个标志位
//该标志位在执行takeContentChanged方法返回true
public void onContentChanged() {
if (mStarted) {
forceLoad();
} else {
//loader已经停止,我们不能立马加载数据,但是记录一下,在开始的时候去更新
mContentChanged = true;
}
}
public Context getContext() {
return mContext;
}
public int getId() {
return mId;
}
public boolean isStarted() {
return mStarted;
}
public boolean isAbandoned() {
return mAbandoned;
}
public boolean isReset() {
return mReset;
}
}
二. LoaderManager源码分析
package android.app;
import android.content.Loader;
import android.os.Bundle;
import android.util.DebugUtils;
import android.util.Log;
import android.util.SparseArray;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.lang.reflect.Modifier;
/**
* 与activity和fragment关联负责管理loader的接口
* 与activity和fragment的生命周期方法配合执行长时间的操作
*/
public abstract class LoaderManager {
/**
* 客户端与loadermanager交互的回调接口
*/
public interface LoaderCallbacks {
/**
* Instantiate and return a new Loader for the given ID.
* @param id The ID whose loader is to be created.
* @param args Any arguments supplied by the caller.
* @return Return a new Loader instance that is ready to start loading.
*/
public Loader onCreateLoader(int id, Bundle args);
/**
*
*/
public void onLoadFinished(Loader loader, D data);
/**
* Called when a previously created loader is being reset, and thus
* making its data unavailable. The application should at this point
* remove any references it has to the Loader's data.
*/
public void onLoaderReset(Loader loader);
}
public abstract Loader initLoader(int id, Bundle args,
LoaderManager.LoaderCallbacks callback);
public abstract Loader restartLoader(int id, Bundle args,
LoaderManager.LoaderCallbacks callback);
public abstract void destroyLoader(int id);
public abstract Loader getLoader(int id);
public abstract void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args);
public static void enableDebugLogging(boolean enabled) {
LoaderManagerImpl.DEBUG = enabled;
}
}
class LoaderManagerImpl extends LoaderManager {
static final String TAG = "LoaderManager";
static boolean DEBUG = false;
// These are the currently active loaders. A loader is here
// from the time its load is started until it has been explicitly
// stopped or restarted by the application.
final SparseArray mLoaders = new SparseArray(0);
// These are previously run loaders. This list is maintained internally
// to avoid destroying a loader while an application is still using it.
// It allows an application to restart a loader, but continue using its
// previously run loader until the new loader's data is available.
final SparseArray mInactiveLoaders = new SparseArray(0);
final String mWho;
boolean mStarted;
boolean mRetaining;
boolean mRetainingStarted;
boolean mCreatingLoader;
private FragmentHostCallback mHost;
final class LoaderInfo implements Loader.OnLoadCompleteListener
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00000000777c3290, pid=5632, tid=6656
#
# JRE version: Java(TM) SE Ru
Spring 中提供一些Aware相关de接口,BeanFactoryAware、 ApplicationContextAware、ResourceLoaderAware、ServletContextAware等等,其中最常用到de匙ApplicationContextAware.实现ApplicationContextAwaredeBean,在Bean被初始后,将会被注入 Applicati
在Java项目中,我们通常会自己写一个DateUtil类,处理日期和字符串的转换,如下所示:
public class DateUtil01 {
private SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public void format(Date d
问题描述:
在实现类中的某一或某几个Override方法发生编译错误如下:
Name clash: The method put(String) of type XXXServiceImpl has the same erasure as put(String) of type XXXService but does not override it
当去掉@Over