LoaderManager - Android 3.0中的新功能

在Android 3.0中提供了一个新概念Loaders,通过LoaderManager类可以很轻松的异步加载数据从Fragment或Activity 中,Loaders提供了回调机制通知最终的运行结果,有点类似AsyncTask类,但由于Loader对于并发可以用过Loader管理器统一管理,所以更适合批量处理多个异步任务的处理(当然内部仍然是多线程)。下面就让Android123一起和大家看下honeycomb中的新特性吧,对于解决多重异步I/O加快Android平板应用的运行是十分有效的。

一、LoaderManager

LoaderManager类位于android.app.LoaderManager,提供了以下几个方法:

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 <D> Loader<D>  getLoader(int id) //返回找到的ID或没有匹配的在Loader中
abstract <D> Loader<D>  initLoader(int id, Bundle args, LoaderCallbacks<D> callback)  //初始化Loader使其成为活动状态
abstract <D> Loader<D>  restartLoader(int id, Bundle args, LoaderCallbacks<D> callback)  //启动一个新的或重启一个存在的Loader在管理器中

同时LoaderManager还有一个回调接口android.app.LoaderManager.LoaderCallbacks<D> 用于和LoaderManager交互:

abstract Loader<D>  onCreateLoader(int id, Bundle args)  //举例并返回一个新Loader通过ID
abstract void  onLoadFinished(Loader<D> loader, D data)  //当前面一个Loader已经完成时回调
abstract void  onLoaderReset(Loader<D> loader)  //当一个新的loader或存在的loader重启时回调

二、Loader

Loader类位于android.content.Loader<D>,整体比较复杂,主要成员有

1. 构造方法  Loader(Context context) //作为唯一实例化方法参数只有一个Context

2. Public Methods

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<D> listener)
void  reset() //重置一个Loader的状态
final void  startLoading()  //启动一个异步的载入从Loader的数据
void  stopLoading()  //停止载入
boolean  takeContentChanged() String  toString()
void  unregisterListener(OnLoadCompleteListener<D> listener)

提供的子类 android.content.Loader.ForceLoadContentObserver 和 接口 android.content.Loader.OnLoadCompleteListener<D>

为了更清晰的表达Android开发网给出一个SDK例子完整代码,来作分析:

 

  1. public class LoaderThrottle extends Activity {  
  2.   static final String TAG = "LoaderThrottle";  
  3.   
  4.   public static final String AUTHORITY = "com.example.android.apis.app.LoaderThrottle";  
  5.   
  6.   public static final class MainTable implements BaseColumns {  
  7.   
  8.       // This class cannot be instantiated  
  9.       private MainTable() {}  
  10.   
  11.       public static final String TABLE_NAME = "main";  
  12.   
  13.       public static final Uri CONTENT_URI =  Uri.parse("content://" + AUTHORITY + "/main");  
  14.   
  15.       public static final Uri CONTENT_ID_URI_BASE  
  16.               = Uri.parse("content://" + AUTHORITY + "/main/");  
  17.   
  18.       public static final String CONTENT_TYPE  
  19.               = "vnd.android.cursor.dir/vnd.example.api-demos-throttle";  
  20.   
  21.       public static final String CONTENT_ITEM_TYPE  
  22.               = "vnd.android.cursor.item/vnd.example.api-demos-throttle";  
  23.       public static final String DEFAULT_SORT_ORDER = "data COLLATE LOCALIZED ASC";  
  24.   
  25.       public static final String COLUMN_NAME_DATA = "data";  
  26.   }  
  27.   
  28. static class DatabaseHelper extends SQLiteOpenHelper {  
  29.   
  30.     private static final String DATABASE_NAME = "loader_throttle.db";  
  31.     private static final int DATABASE_VERSION = 2;  
  32.   
  33.     DatabaseHelper(Context context) {  
  34.   
  35.         // calls the super constructor, requesting the default cursor factory.  
  36.         super(context, DATABASE_NAME, null, DATABASE_VERSION);  
  37.     }  
  38.   
  39.     @Override  
  40.     public void onCreate(SQLiteDatabase db) {  
  41.         db.execSQL("CREATE TABLE " + MainTable.TABLE_NAME + " ("  
  42.                 + MainTable._ID + " INTEGER PRIMARY KEY,"  
  43.                 + MainTable.COLUMN_NAME_DATA + " TEXT"  
  44.                 + ");");  
  45.     }  
  46.   
  47.     @Override  
  48.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
  49.   
  50.         // Logs that the database is being upgraded  
  51.         Log.w(TAG, "Upgrading database from version " + oldVersion + " to "  
  52.                 + newVersion + ", which will destroy all old data");  
  53.   
  54.         // Kills the table and existing data  
  55.         db.execSQL("DROP TABLE IF EXISTS notes");  
  56.   
  57.         // Recreates the database with a new version  
  58.         onCreate(db);  
  59.     }  
  60. }  
  61.   
  62.   public static class SimpleProvider extends ContentProvider {  
  63.       // A projection map used to select columns from the database  
  64.       private final HashMap<String, String> mNotesProjectionMap;  
  65.       // Uri matcher to decode incoming URIs.  
  66.       private final UriMatcher mUriMatcher;  
  67.   
  68.       // The incoming URI matches the main table URI pattern  
  69.       private static final int MAIN = 1;  
  70.       // The incoming URI matches the main table row ID URI pattern  
  71.       private static final int MAIN_ID = 2;  
  72.   
  73.       // Handle to a new DatabaseHelper.  
  74.       private DatabaseHelper mOpenHelper;  
  75.   
  76.       public SimpleProvider() {  
  77.           // Create and initialize URI matcher.  
  78.           mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);  
  79.           mUriMatcher.addURI(AUTHORITY, MainTable.TABLE_NAME, MAIN);  
  80.           mUriMatcher.addURI(AUTHORITY, MainTable.TABLE_NAME + "/#", MAIN_ID);  
  81.   
  82.           // Create and initialize projection map for all columns.  This is  
  83.           // simply an identity mapping.  
  84.           mNotesProjectionMap = new HashMap<String, String>();  
  85.           mNotesProjectionMap.put(MainTable._ID, MainTable._ID);  
  86.           mNotesProjectionMap.put(MainTable.COLUMN_NAME_DATA, MainTable.COLUMN_NAME_DATA);  
  87.       }  
  88.   
  89.       @Override  
  90.       public boolean onCreate() {  
  91.           mOpenHelper = new DatabaseHelper(getContext());  
  92.           // Assumes that any failures will be reported by a thrown exception.  
  93.           return true;  
  94.       }  
  95.   
  96.       @Override  
  97.       public Cursor query(Uri uri, String[] projection, String selection,  
  98.               String[] selectionArgs, String sortOrder) {  
  99.   
  100.           // Constructs a new query builder and sets its table name  
  101.           SQLiteQueryBuilder qb = new SQLiteQueryBuilder();  
  102.           qb.setTables(MainTable.TABLE_NAME);  
  103.   
  104.           switch (mUriMatcher.match(uri)) {  
  105.               case MAIN:  
  106.                   // If the incoming URI is for main table.  
  107.                   qb.setProjectionMap(mNotesProjectionMap);  
  108.                   break;  
  109.   
  110.               case MAIN_ID:  
  111.                   // The incoming URI is for a single row.  
  112.                   qb.setProjectionMap(mNotesProjectionMap);  
  113.                   qb.appendWhere(MainTable._ID + "=?");  
  114.                   selectionArgs = DatabaseUtils.appendSelectionArgs(selectionArgs,  
  115.                           new String[] { uri.getLastPathSegment() });  
  116.                   break;  
  117.   
  118.               default:  
  119.                   throw new IllegalArgumentException("Unknown URI " + uri);  
  120.           }  
  121.   
  122.   
  123.           if (TextUtils.isEmpty(sortOrder)) {  
  124.               sortOrder = MainTable.DEFAULT_SORT_ORDER;  
  125.           }  
  126.   
  127.           SQLiteDatabase db = mOpenHelper.getReadableDatabase();  
  128.   
  129.           Cursor c = qb.query(db, projection, selection, selectionArgs,  
  130.                   null /* no group */null /* no filter */, sortOrder);  
  131.   
  132.           c.setNotificationUri(getContext().getContentResolver(), uri);  
  133.           return c;  
  134.       }  
  135.   
  136.       @Override  
  137.       public String getType(Uri uri) {  
  138.           switch (mUriMatcher.match(uri)) {  
  139.               case MAIN:  
  140.                   return MainTable.CONTENT_TYPE;  
  141.               case MAIN_ID:  
  142.                   return MainTable.CONTENT_ITEM_TYPE;  
  143.               default:  
  144.                   throw new IllegalArgumentException("Unknown URI " + uri);  
  145.           }  
  146.       }  
  147.   
  148.       @Override  
  149.       public Uri insert(Uri uri, ContentValues initialValues) {  
  150.           if (mUriMatcher.match(uri) != MAIN) {  
  151.               // Can only insert into to main URI.  
  152.               throw new IllegalArgumentException("Unknown URI " + uri);  
  153.           }  
  154.   
  155.           ContentValues values;  
  156.   
  157.           if (initialValues != null) {  
  158.               values = new ContentValues(initialValues);  
  159.           } else {  
  160.               values = new ContentValues();  
  161.           }  
  162.   
  163.           if (values.containsKey(MainTable.COLUMN_NAME_DATA) == false) {  
  164.               values.put(MainTable.COLUMN_NAME_DATA, "");  
  165.           }  
  166.   
  167.           SQLiteDatabase db = mOpenHelper.getWritableDatabase();  
  168.   
  169.           long rowId = db.insert(MainTable.TABLE_NAME, null, values);  
  170.   
  171.           // If the insert succeeded, the row ID exists.  
  172.           if (rowId > 0) {  
  173.               Uri noteUri = ContentUris.withAppendedId(MainTable.CONTENT_ID_URI_BASE, rowId);  
  174.               getContext().getContentResolver().notifyChange(noteUri, null);  
  175.               return noteUri;  
  176.           }  
  177.   
  178.           throw new SQLException("Failed to insert row into " + uri);  
  179.       }  
  180.   
  181.       @Override  
  182.       public int delete(Uri uri, String where, String[] whereArgs) {  
  183.           SQLiteDatabase db = mOpenHelper.getWritableDatabase();  
  184.           String finalWhere;  
  185.   
  186.           int count;  
  187.   
  188.           switch (mUriMatcher.match(uri)) {  
  189.               case MAIN:  
  190.                   count = db.delete(MainTable.TABLE_NAME, where, whereArgs);  
  191.                   break;  
  192.   
  193.               case MAIN_ID:  
  194.                   finalWhere = DatabaseUtils.concatenateWhere(  
  195.                           MainTable._ID + " = " + ContentUris.parseId(uri), where);  
  196.                   count = db.delete(MainTable.TABLE_NAME, finalWhere, whereArgs);  
  197.                   break;  
  198.   
  199.               default:  
  200.                   throw new IllegalArgumentException("Unknown URI " + uri);  
  201.           }  
  202.   
  203.           getContext().getContentResolver().notifyChange(uri, null);  
  204.   
  205.           return count;  
  206.       }  
  207.   
  208.       @Override  
  209.       public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {  
  210.           SQLiteDatabase db = mOpenHelper.getWritableDatabase();  
  211.           int count;  
  212.           String finalWhere;  
  213.   
  214.           switch (mUriMatcher.match(uri)) {  
  215.               case MAIN:  
  216.                   // If URI is main table, update uses incoming where clause and args.  
  217.                   count = db.update(MainTable.TABLE_NAME, values, where, whereArgs);  
  218.                   break;  
  219.   
  220.               case MAIN_ID:  
  221.                   // If URI is for a particular row ID, update is based on incoming  
  222.                   // data but modified to restrict to the given ID.  
  223.                   finalWhere = DatabaseUtils.concatenateWhere(  
  224.                           MainTable._ID + " = " + ContentUris.parseId(uri), where);  
  225.                   count = db.update(MainTable.TABLE_NAME, values, finalWhere, whereArgs);  
  226.                   break;  
  227.   
  228.               default:  
  229.                   throw new IllegalArgumentException("Unknown URI " + uri);  
  230.           }  
  231.   
  232.           getContext().getContentResolver().notifyChange(uri, null);  
  233.   
  234.           return count;  
  235.       }  
  236.   }  
  237.   
  238.   @Override  
  239.   protected void onCreate(Bundle savedInstanceState) {  
  240.       super.onCreate(savedInstanceState);  
  241.   
  242.       FragmentManager fm = getFragmentManager();  
  243.   
  244.       if (fm.findFragmentById(android.R.id.content) == null) {  
  245.           ThrottledLoaderListFragment list = new ThrottledLoaderListFragment();  
  246.           fm.beginTransaction().add(android.R.id.content, list).commit();  
  247.       }  
  248.   }  
  249.   
  250.   public static class ThrottledLoaderListFragment extends ListFragment  
  251.           implements LoaderManager.LoaderCallbacks<Cursor> {  
  252.   
  253.     static final int POPULATE_ID = Menu.FIRST;  
  254.       static final int CLEAR_ID = Menu.FIRST+1;  
  255.   
  256.       SimpleCursorAdapter mAdapter;  
  257.   
  258.       String mCurFilter;  
  259.   
  260.       AsyncTask<Void, Void, Void> mPopulatingTask;  
  261.   
  262.       @Override public void onActivityCreated(Bundle savedInstanceState) {  
  263.           super.onActivityCreated(savedInstanceState);  
  264.   
  265.           setEmptyText("No data.  Select 'Populate' to fill with data from Z to A at a rate of 4 per second.");  
  266.           setHasOptionsMenu(true);  
  267.   
  268.           // Create an empty adapter we will use to display the loaded data.  
  269.           mAdapter = new SimpleCursorAdapter(getActivity(),  
  270.                   android.R.layout.simple_list_item_1, null,  
  271.                   new String[] { MainTable.COLUMN_NAME_DATA },  
  272.                   new int[] { android.R.id.text1 }, 0);  
  273.           setListAdapter(mAdapter);  
  274.   
  275.           // Prepare the loader.  Either re-connect with an existing one,  
  276.           // or start a new one.  
  277.           getLoaderManager().initLoader(0nullthis);  
  278.       }  
  279.   
  280.       @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {  
  281.           menu.add(Menu.NONE, POPULATE_ID, 0"Populate")  
  282.                   .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);  
  283.           menu.add(Menu.NONE, CLEAR_ID, 0"Clear")  
  284.                   .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);  
  285.       }  
  286.   
  287.       @Override public boolean onOptionsItemSelected(MenuItem item) {  
  288.           final ContentResolver cr = getActivity().getContentResolver();  
  289.   
  290.           switch (item.getItemId()) {  
  291.               case POPULATE_ID:  
  292.                   if (mPopulatingTask != null) {  
  293.                       mPopulatingTask.cancel(false);  
  294.                   }  
  295.                   mPopulatingTask = new AsyncTask<Void, Void, Void>() {  
  296.                       @Override protected Void doInBackground(Void... params) {  
  297.                           for (char c='Z'; c>='A'; c--) {  
  298.                               if (isCancelled()) {  
  299.                                   break;  
  300.                               }  
  301.                               StringBuilder builder = new StringBuilder("Data ");  
  302.                               builder.append(c);  
  303.                               ContentValues values = new ContentValues();  
  304.                               values.put(MainTable.COLUMN_NAME_DATA, builder.toString());  
  305.                               cr.insert(MainTable.CONTENT_URI, values);  
  306.                               // Wait a bit between each insert.  
  307.                               try {  
  308.                                   Thread.sleep(250);  
  309.                               } catch (InterruptedException e) {  
  310.                               }  
  311.                           }  
  312.                           return null;  
  313.                       }  
  314.                   };  
  315.                   mPopulatingTask.executeOnExecutor(  
  316.                           AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null);  
  317.                   return true;  
  318.   
  319.               case CLEAR_ID:  
  320.                   if (mPopulatingTask != null) {  
  321.                       mPopulatingTask.cancel(false);  
  322.                       mPopulatingTask = null;  
  323.                   }  
  324.                   AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {  
  325.                       @Override protected Void doInBackground(Void... params) {  
  326.                           cr.delete(MainTable.CONTENT_URI, nullnull);  
  327.                           return null;  
  328.                       }  
  329.                   };  
  330.                   task.execute((Void[])null);  
  331.                   return true;  
  332.   
  333.               default:  
  334.                   return super.onOptionsItemSelected(item);  
  335.           }  
  336.       }  
  337.   
  338.       @Override public void onListItemClick(ListView l, View v, int position, long id) {  
  339.           // Insert desired behavior here.  
  340.           Log.i(TAG, "Item clicked: " + id);  
  341.       }  
  342.   
  343.       // These are the rows that we will retrieve.  
  344.       static final String[] PROJECTION = new String[] {  
  345.           MainTable._ID,  
  346.           MainTable.COLUMN_NAME_DATA,  
  347.       };  
  348.   
  349.       public Loader<Cursor> onCreateLoader(int id, Bundle args) {  
  350.           CursorLoader cl = new CursorLoader(getActivity(), MainTable.CONTENT_URI,  
  351.                   PROJECTION, nullnullnull);  
  352.           cl.setUpdateThrottle(2000); // update at most every 2 seconds.  
  353.           return cl;  
  354.       }  
  355.   
  356.       public void onLoadFinished(Loader<Cursor> loader, Cursor data) {  
  357.           mAdapter.swapCursor(data);  
  358.       }  
  359.   
  360.       public void onLoaderReset(Loader<Cursor> loader) {  
  361.           mAdapter.swapCursor(null);  
  362.       }  
  363.   } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值