在Android 3.0中提供了一个新概念Loaders,通过LoaderManager类可以很轻松的异步加载数据从Fragment或Activity 中,Loaders提供了回调机制通知最终的运行结果,有点类似AsyncTask类,但由于Loader对于并发可以用过Loader管理器统一管理,所以更适合批量处理多个异步任务的处理(当然内部仍然是多线程)。下面就一起和大家看下honeycomb中的新特性吧,对于解决多重异步I/O加快Android平板应用的运行是十分有效的。 一、LoaderManager LoaderManager类位于android.app.LoaderManager,提供了以下几个方法java代码:
abstract void destroyLoader(int id) //停止并移除loader通过ID
abstract void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) //打印LoaderManager的状态到一个流中
static void enableDebugLogging(boolean enabled) //启用debug记录
abstract
abstract
abstract
复制代码
同时LoaderManager还有一个回调接口android.app.LoaderManager.LoaderCallbacks 用于和LoaderManager交互java代码:void abandon() //高速Loader他在绑定
String dataToString(D data) //用于调试,转换一个Loader数据类的实例为字符串用于打印
void deliverResult(D data) //发送一个load注册的listener结果
void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) //打印loader状态通过给定的流
void forceLoad() //强制一个异步载入
Context getContext() //返回Context实例
int getId()
boolean isAbandoned() //判断是否已经绑定
boolean isReset() //判断是否已经重启
boolean isStarted() //判断是否已经执行
void onContentChanged() //内容变化回调
registerListener(int id, OnLoadCompleteListener
void reset() //重置一个Loader的状态
final void startLoading() //启动一个异步的载入从Loader的数据
void stopLoading() //停止载入
boolean takeContentChanged() String toString()
void unregisterListener(OnLoadCompleteListener
复制代码
提供的子类 android.content.Loader.ForceLoadContentObserver 和 接口 android.content.Loader.OnLoadCompleteListener为了更清晰的表达Android开发网给出一个SDK例子完整代码,来作分析java代码:public class LoaderThrottle extends Activity {
static final String TAG = "LoaderThrottle";
public static final String AUTHORITY = "com.example.android.apis.app.LoaderThrottle";
public static final class MainTable implements BaseColumns {
// This class cannot be instantiated
private MainTable() {}
public static final String TABLE_NAME = "main";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/main");
public static final Uri CONTENT_ID_URI_BASE
= Uri.parse("content://" + AUTHORITY + "/main/");
public static final String CONTENT_TYPE
= "vnd.android.cursor.dir/vnd.example.api-demos-throttle";
public static final String CONTENT_ITEM_TYPE
= "vnd.android.cursor.item/vnd.example.api-demos-throttle";
public static final String DEFAULT_SORT_ORDER = "data COLLATE LOCALIZED ASC";
public static final String COLUMN_NAME_DATA = "data";
}
static class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "loader_throttle.db";
private static final int DATABASE_VERSION = 2;
DatabaseHelper(Context context) {
// calls the super constructor, requesting the default cursor factory.
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + MainTable.TABLE_NAME + " ("
- MainTable._ID + " INTEGER PRIMARY KEY,"
- MainTable.COLUMN_NAME_DATA + " TEXT"
- ");");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Logs that the database is being upgraded
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
- newVersion + ", which will destroy all old data");
// Kills the table and existing data
db.execSQL("DROP TABLE IF EXISTS notes");
// Recreates the database with a new version
onCreate(db);
}
}
复制代码
系列之浅谈Android 3.0中的LoaderManager(二)的帖子链接http://www.eoeandroid.com/thread-101847-1-1.html系列之浅谈Android 3.0中的LoaderManager(三)的帖子链接http://www.eoeandroid.com/thread-101848-1-1.html