Android LoaderManager 和 Loader 初步使用
Android 3.0中提供了一個新概念Loaders,這兩天看了之前Contacts的原始碼,其中自己寫了通話記錄部分,發現原始碼裡面有很多LoaderManager的使用,現在做一個總結,記錄一下LoaderManager的基本用法:
這個其實是分為兩部分:LoaderManager和自定義Loader,比如像載入聯絡人,簡訊這些系統提供ContentProvider的資料時,其實就是返回一個標準cursor,那我們就沒必要自定義loader 直接用loaderManager來管理這個CursorLoader,這是一種簡單普遍的情況。還有一種就是我們要載入非cursor型別的資料時,就必須自定義loader了。
1. LoaderManager和CursorLoader
舉例用一個listFragment載入聯絡人,那麼讓它實現LoaderManager.LoaderCallbacks<Cursor> 介面,必須重寫下面3個方法:
onCreateLoader: 這個是建立一個CursorLoader並返回,我們在裡面new一個CursorLoader並返回就OK了
onLoadFinished: 這個是載入完成後更新UI,在這裡就是setAdapter了 而這個載入過程其實就是在CursorLoader裡面完成的,只不過系統幫我們完成了,而如果自定義loader的話就要自己完成,這就是區別!
onLoaderReset: loader的重置,在這裡一般讓UI不顯示資料就行
就這麼3個方法就可以完成資料載入了,核心就是把CursorLoader構造出來,至於載入過程,資料監聽更新這些就有系統去完成吧!
public class ContactsFragment extends ListFragment implements LoaderManager.LoaderCallbacks <Cursor>{ private SimpleCursorAdapter mAdapter; private final String[] CONTACTS_PROJECTION = new String[]{Contacts._ID, Co<span style="font-size:14px;">ntacts.DISPL</span>AY_NAME, Contacts.CONTACT_STATUS, Contacts.CONTACT_PRESENCE, Contacts.PHOTO_ID, Contacts.LOOKUP_KEY, }; @Override public void onActivityCreated(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onActivityCreated(savedInstanceState); setEmptyText("當前沒有聯絡人"); mAdapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_2, null, new String[] { Contacts.DISPLAY_NAME, Contacts.CONTACT_STATUS }, new int[] { android.R.id.text1, android.R.id.text2 }, 0); setListAdapter(mAdapter); getLoaderManager().initLoader(1, null, this); } @Override public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) { // TODO Auto-generated method stub String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" + Contacts.HAS_PHONE_NUMBER + "=1) AND (" + Contacts.DISPLAY_NAME + " != '' ))"; return new CursorLoader(getActivity(), Contacts.CONTENT_URI, <span style="font-size:14px;"> </span>CONTACTS_PROJECTION, select, null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"); } @Override public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) { // TODO Auto-generated method stub mAdapter.swapCursor(arg1); } @Override public void onLoaderReset(Loader<Cursor> arg0) { // TODO Auto-generated method stub mAdapter.swapCursor(null); } }
2. 自定義Loader
但有些拿不到cursor這樣的資料,我們就需要自定義Loader,其實在Loader裡面只需要重寫兩個方法:一個onStartLoading,還有一個loadInBackground,但為了程式碼更加完善一些,再重寫另外幾個方法: onStopLoading onReset onCanceled,可以類比activity的生命週期,然後參考google文件進行重寫,先貼出程式碼:
public class MySmsLoader extends AsyncTaskLoader<List<SmsEntry>> { private PackageManager mPm; private InstalledAppsObserver mAppsObserver; public MySmsLoader(Context context) { super(context); // TODO Auto-generated constructor stub mPm = getContext().getPackageManager(); Log.d("dml", "new一個MySmsLoader"); } @Override public List<SmsEntry> loadInBackground() { Log.d("dml", "loadInBackground--------" ); // TODO Auto-generated method stub // Retrieve all installed applications. List<ApplicationInfo> apps = mPm.getInstalledApplications(0); Log.d("dml", "apps >>>>>>>>>>" + apps); if (apps == null) { Log.d("dml", "apps null"); apps = new ArrayList<ApplicationInfo>(); } // Create corresponding array of entries and load their labels. List<SmsEntry> entries = new ArrayList<SmsEntry>(apps.size()); for (int i = 0; i < apps.size(); i++) { SmsEntry entry = new SmsEntry(); entry.setFrom_name(i+"::::::::"); entry.setSend_time(apps.get(i).packageName); entries.add(entry); } Log.d("dml", "entries---->>>>>>>>>----" + entries); return entries; } @Override protected void onStartLoading() { // TODO Auto-generated method stub super.onStartLoading(); if (mAppsObserver == null) { mAppsObserver = new InstalledAppsObserver(this); } forceLoad(); } @Override protected void onStopLoading() { // The Loader has been put in a stopped state, so we should attempt to // cancel the current load (if there is one). cancelLoad(); // Note that we leave the observer as is; Loaders in a stopped state // should still monitor the data source for changes so that the Loader // will know to force a new load if it is ever started again. } @Override protected void onReset() { // Ensure the loader is stopped. onStopLoading(); // The Loader is being reset, so we should stop monitoring for changes. if (mAppsObserver != null) { getContext().unregisterReceiver(mAppsObserver); mAppsObserver = null; } } @Override public void onCanceled(List<SmsEntry> apps) { // Attempt to cancel the current asynchronous load. super.onCanceled(apps); // The load has been canceled, so we should release the resources // associated with 'mApps'. releaseResources(apps); } private void releaseResources(List<SmsEntry> apps) { // For a simple List, there is nothing to do. For something like a Cursor, // we would close it in this method. All resources associated with the // Loader should be released here. } }
onStartLoading:註冊一些監聽器到loader上,並且執行一次forceLoad(); 否則loader不會開始工作
loadInBackground:不用說,在這裡就是載入資料並且返回,其實這個資料就返回到了LoaderManager的
onLoadFinished方法第二個引數
onStopLoading:停止載入資料,但不要停止監聽也不要釋放資料,就可以隨時重啟loader
onReset:先確保已經停止載入資料了,然後釋放掉監聽器並設為null
onCanceled: 在這裡可以釋放資源,如果是list就不需要做什麼了,但是象cursor或者開啟了什麼檔案就應該關閉一下
這幾個生命週期方法還不是非常清楚,但就象activity的生命週期一樣,可以裡面的細節不是很瞭解 但我們還是可以很熟練的使用activity,除了這幾個還有一個deliverResult(),確實沒弄懂,留到以後在體會吧!
下面就是監聽器的程式碼,注意是在構造方法中繫結到loader:
public class InstalledAppsObserver extends BroadcastReceiver { private static final String TAG = "ADP_InstalledAppsObserver"; private static final boolean DEBUG = true; private MySmsLoader mLoader; public InstalledAppsObserver(MySmsLoader loader) { mLoader = loader; // Register for events related to application installs/removals/updates. IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); filter.addAction(Intent.ACTION_PACKAGE_REMOVED); filter.addAction(Intent.ACTION_PACKAGE_CHANGED); filter.addDataScheme("package"); mLoader.getContext().registerReceiver(this, filter); // Register for events related to sdcard installation. IntentFilter sdFilter = new IntentFilter(); sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE); sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); mLoader.getContext().registerReceiver(this, sdFilter); } @Override public void onReceive(Context context, Intent intent) { if (DEBUG) Log.i(TAG, "+++ The observer has detected an application change!" + " Notifying Loader... +++"); // Tell the loader about the change. mLoader.onContentChanged(); } }
剩下的工作就和之前一樣了,在LoaderManager裡面拿到loader返回的資料,更新UI:
public class SmsFragment extends ListFragment implements LoaderCallbacks <List<SmsEntry>>{ private SmsAdapter adapter; @Override public void onActivityCreated(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onActivityCreated(savedInstanceState); Log.d("dml", "進入SmsFragment "); adapter = new SmsAdapter(getActivity()); setEmptyText("No applications"); setListAdapter(adapter); setListShown(false); getLoaderManager().initLoader(2, null, this); } @Override public Loader<List<SmsEntry>> onCreateLoader(int arg0, Bundle arg1) { // TODO Auto-generated method stub Log.d("dml", " onCreateLoader"); return new MySmsLoader(getActivity()); } @Override public void onLoadFinished(Loader<List<SmsEntry>> arg0, List<SmsEntry> arg1) { // TODO Auto-generated method stub Log.d("dml", " onLoadFinished>>>>>>>arg1: " + arg1); adapter.setData(arg1); if (isResumed()) { setListShown(true); } else { setListShownNoAnimation(true); } } @Override public void onLoaderReset(Loader<List<SmsEntry>> arg0) { // TODO Auto-generated method stub Log.d("dml", " onLoaderReset"); adapter.setData(null); } class SmsAdapter extends ArrayAdapter<SmsEntry>{ private LayoutInflater inflater; public SmsAdapter(Context context) { super(context,android.R.layout.simple_list_item_2); // TODO Auto-generated constructor stub Log.d("dml", " new SmsAdapter--------"); inflater = (LayoutInflater) context.getSystemService (context.LAYOUT_INFLATER_SERVICE); } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub Log.d("dml", "getView-------- "); View view = convertView; ViewHolder holder; if(view==null){ view = inflater.inflate(R.layout.item, parent,false); holder = new ViewHolder(); holder.name = (TextView) view.findViewById(R.id.name); holder.date = (TextView)view.findViewById(R.id.date); view.setTag(holder); } else{ holder = (ViewHolder)view.getTag(); } SmsEntry item = getItem(position); Log.d("dml", "item-------- " + item); holder.name.setText(item.from_name); holder.date.setText(item.send_time); return view; } private class ViewHolder{ TextView name; TextView date; } public void setData(List<SmsEntry> data){ clear(); if(data!=null){ Log.d("dml", "data-------- " + data); for(int i=0;i<data.size();i++){ add(data.get(i)); } } } } }
可以看出這裡的資料結構由原來的cursor變成了自定義的SmsEntry,另外在這裡要學會自定義ArrayAdapter的用法,顯示資料
相關文章
- Android開發之Loader與LoaderManagerAndroid
- Android中Loader及LoaderManager的使用(附原始碼下載)Android原始碼
- Loader 知識梳理(1) LoaderManager初探
- 深入原始碼解析Android中Loader、AsyncTaskLoader、CursorLoader、LoaderManager原始碼Android
- Android-Universal-Image-Loader 框架使用Android框架
- java註解初步學習和使用Java
- 6使用 loader 處理 CSS 和 SassCSS
- Android MVP+LoaderManager+CursorLoader實現圖片搜尋AndroidMVP
- Ndk 初步開發 In AndroidAndroid
- Android 元件系列-----Activity初步Android元件
- 使用SQL*Loader匯入CLOB和BLOB資料使用案例SQL
- sql loader使用例子SQL
- Android中-Loader載入器Android
- Android Loader原始碼分析(二)Android原始碼
- Statspack初步學和用第三篇 分析初步
- SQL*Loader使用方法SQL
- oracle sql loader 的使用OracleSQL
- JAVA POI的初步使用Java
- HttpClient初步使用方法HTTPclient
- weblogic 管理視訊1-安裝和初步使用Web
- webpack loader和plugin編寫WebPlugin
- Android知識點回顧之LoaderAndroid
- babel-loader 使用指南Babel
- Oracle SQL*Loader使用案例(一)OracleSQL
- Oracle SQL*Loader使用案例(二)OracleSQL
- Oracle SQL*Loader使用案例(三)OracleSQL
- Oracle SQL*Loader使用案例(四)OracleSQL
- SQL*Loader的使用方法SQL
- 使用sql loader導資料SQL
- Oracle SQL*Loader 使用指南OracleSQL
- SQL LOADER的使用方法SQL
- SQL*LOADER 的使用小結SQL
- 1.Appium初步使用APP
- Statspack初步學和用第一篇 安裝初步
- Android初步進階之Design Support Library庫簡單使用(一)Android
- 說說Loader和Plugin的區別?編寫Loader,Plugin的思路?Plugin
- Android 非同步載入神器Loader全解析Android非同步
- Android 非同步載入神器 Loader 全解析Android非同步