Android異步加載神器Loader全解析
作者:網絡轉載 發布時間:[ 2015/10/9 13:13:33 ] 推薦標簽:移動平臺測試 移動測試
在之前呢,我們經常會有這種需求,比如在某個activity,或者某個fragment里面,我們需要查找某個數據源,并且顯示出來,當數據源自己更新的時候,界面也要及時響應。
當然咯,查找數據這個過程可能很短,但是也可能很漫長,為了避免anr,我們都是開啟一個子線程去查找,然后通過handler來更新我們的ui界面。但是,考慮到activity和
fragment 復雜的生命周期,上述的方法 使用起來會很不方便,畢竟你要考慮到保存現場 還原現場 等等復雜的工作來保證你的app無懈可擊。所以后來呢谷歌幫我們推出了一個新的東西—Loader。他可以幫我們完成上述所有功能!實在是很強大。
如果你有閱讀英文技術文檔的習慣 那么谷歌官方的文檔 也許比我所說的更加完美。具體可以參考如下:
http://developer.android.com/intl/zh-cn/reference/android/app/LoaderManager.html
http://developer.android.com/intl/zh-cn/reference/android/content/AsyncTaskLoader.html
http://developer.android.com/intl/zh-cn/guide/components/loaders.html
我所述的內容也是主要基于上述三篇文檔。
首先呢,我們來看第一個例子,這個例子也是官方的推薦了,我給簡化了一下,主要是監聽手機里 聯系人這個數據源。當數據源改變的時候 自動update 我們的ui。
package com.example.administrator.modifytestview;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.ListFragment;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getFragmentManager();
CursorLoaderListFragment list = new CursorLoaderListFragment();
fm.beginTransaction().replace(R.id.root, list).commit();
}
public static class CursorLoaderListFragment extends ListFragment
implements LoaderManager.LoaderCallbacks<Cursor> {
// This is the Adapter being used to display the list's data.
SimpleCursorAdapter mAdapter;
// If non-null, this is the current filter the user has provided.
String mCurFilter;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
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);
//這個地方初始化了我們的loader
getLoaderManager().initLoader(0, null, this);
super.onActivityCreated(savedInstanceState);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Insert desired behavior here.
Log.i("FragmentComplexList", "Item clicked: " + id);
}
// These are the Contacts rows that we will retrieve.
static final String[] CONTACTS_SUMMARY_PROJECTION = new String[]{
Contacts._ID,
Contacts.DISPLAY_NAME,
Contacts.CONTACT_STATUS,
Contacts.CONTACT_PRESENCE,
Contacts.PHOTO_ID,
Contacts.LOOKUP_KEY,
};
//只會調用一次
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// This is called when a new Loader needs to be created. This
// sample only has one Loader, so we don't care about the ID.
// First, pick the base URI to use depending on whether we are
// currently filtering.
Uri baseUri;
if (mCurFilter != null) {
baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI,
Uri.encode(mCurFilter));
} else {
baseUri = Contacts.CONTENT_URI;
}
// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
+ Contacts.HAS_PHONE_NUMBER + "=1) AND ("
+ Contacts.DISPLAY_NAME + " != '' ))";
//返回的是對這個數據源的監控
return new CursorLoader(getActivity(), baseUri,
CONTACTS_SUMMARY_PROJECTION, select, null,
Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
}
//每次數據源都有更新的時候,會回調這個方法,然后update 我們的ui了。
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Swap the new cursor in. (The framework will take care of closing the
// old cursor once we return.)
mAdapter.swapCursor(data);
// The list should now be shown.
if (isResumed()) {
setListShown(true);
} else {
setListShownNoAnimation(true);
}
}
public void onLoaderReset(Loader<Cursor> loader) {
// This is called when the last Cursor provided to onLoadFinished()
// above is about to be closed. We need to make sure we are no
// longer using it.
mAdapter.swapCursor(null);
}
}
}
可以仔細的觀察一下這個代碼,我們能發現 使用loader所需要的一些步驟:
1.需要一個activity或者是fragment,當然在上述的例子里 我們使用的是fragment。
2.一個LoaderManger的實例,注意看53行,我們get了一個loadermanager。這個地方是獲取實例了。
3.需要一個CursorLoader,并且從contentProvider獲取數據源,90-97行 是這么做的。
4.需要實現一個LoaderCallBack的這個接口,然后在幾個回調方法里 寫上我們自己業務的邏輯 即可。你看34行是繼承的接口。
還有3個回調方法在那,我們都在里面實現了自己的邏輯。
到這,其實一看,思路還是很清晰的。那到這里 有人肯定要說了。你這個沒用啊,要實現contentprovider,我們的app不需要做數據共享的,能否直接操作數據庫呢?答案是可以的。在這里我們也可以構造出一個場景。假設有一張學生表。我們點擊add按鈕,自動往這個表里面增加一個數據,然后下面有個listview 會自動捕捉到 這個數據源的變化,然后自動更新列表。
我們可以知道 上面那個demo里面 CursorLoader的定義是這樣的
public class CursorLoader extends AsyncTaskLoader<Cursor> {
我們現在要實現一個不用contentProvider的Loader 也是基于AsyncTaskLoader來的。
先給出一個抽象類:
package com.example.administrator.activeandroidtest3;
import android.content.AsyncTaskLoader;
import android.content.Context;
import android.database.Cursor;
public abstract class SimpleCursorLoader extends AsyncTaskLoader<Cursor> {
private Cursor mCursor;
public SimpleCursorLoader(Context context) {
super(context);
}
/* 在子線程里運作 */
@Override
public abstract Cursor loadInBackground();
/* 在ui 線程里運作 */
@Override
public void deliverResult(Cursor cursor) {
if (isReset()) {
// An async query came in while the loader is stopped
if (cursor != null) {
cursor.close();
}
return;
}
Cursor oldCursor = mCursor;
mCursor = cursor;
if (isStarted()) {
super.deliverResult(cursor);
}
if (oldCursor != null && oldCursor != cursor && !oldCursor.isClosed()) {
oldCursor.close();
}
}
@Override
protected void onStartLoading() {
if (mCursor != null) {
deliverResult(mCursor);
}
if (takeContentChanged() || mCursor == null) {
forceLoad();
}
}
@Override
protected void onStopLoading() {
cancelLoad();
}
@Override
public void onCanceled(Cursor cursor) {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
@Override
protected void onReset() {
super.onReset();
onStopLoading();
if (mCursor != null && !mCursor.isClosed()) {
mCursor.close();
}
mCursor = null;
}
}
然后我們再接著定義我們終的 不需要provider的loader實現類(注意你如果想寫的比較完美的話 cursor記得用抽象類的,抽象類的那個不要寫成private的了,我這里為了圖簡單 直接用自己構造的)。
package com.example.administrator.activeandroidtest3;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
/**
* Created by Administrator on 2015/10/7.
*/
public class SpecialLoader extends SimpleCursorLoader {
ForceLoadContentObserver mObserver = new ForceLoadContentObserver();
private Context context;
public SpecialLoader(Context context) {
super(context);
this.context = context;
}
@Override
public Cursor loadInBackground() {
DatabaseHelper dh = new DatabaseHelper(context, "Test.db");
SQLiteDatabase database = dh.getReadableDatabase();
String table = "Student";
String[] columns = new String[]{"Name", "No"};
//這個地方因為我用的是activeandroid 的orm 框架,所以默認的自增長主鍵是Id,但是SimpleCursorAdapter
//需要的是_id 否則會報錯,所以這里要重命名一下
Cursor cursor = database.rawQuery("SELECT Id AS _id,Name,No FROM Student", null);
if (database != null) {
if (cursor != null) {
//注冊一下這個觀察者
cursor.registerContentObserver(mObserver);
//這邊也要注意 一定要監聽這個uri的變化。但是如果你這個uri沒有對應的provider的話
//記得在你操作數據庫的時候 通知一下這個uri
cursor.setNotificationUri(context.getContentResolver(), MainActivity.uri);
}
}
return cursor;
}
}
然后我們在簡單看下activity 主類里的代碼:
package com.example.administrator.activeandroidtest3;
import android.app.Activity;
import android.app.LoaderManager;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import com.activeandroid.query.Select;
import java.util.List;
import java.util.Random;
public class MainActivity extends Activity implements LoaderManager.LoaderCallbacks {
public static final Uri uri = Uri.parse("content://com.example.student");
private TextView addTv;
private ListView lv;
private SimpleCursorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addTv = (TextView) this.findViewById(R.id.add);
addTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Student student = new Student();
student.name = getRandomString(5);
student.no = (int) (Math.random() * 1000) + "";
student.sex = (int) (Math.random() * 1);
student.save();
//操作完數據庫要notify 不然loader那邊收不到哦
getContentResolver().notifyChange(uri, null);
}
});
lv = (ListView) this.findViewById(R.id.lv);
adapter = new SimpleCursorAdapter(MainActivity.this,
android.R.layout.simple_list_item_2, null,
new String[]{"Name", "No"},
new int[]{android.R.id.text1, android.R.id.text2}, 0);
lv.setAdapter(adapter);
getLoaderManager().initLoader(0, null, this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static String getRandomString(int length) { //length表示生成字符串的長度
String base = "abcdefghijklmnopqrstuvwxyz0123456789";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
int number = random.nextInt(base.length());
sb.append(base.charAt(number));
}
return sb.toString();
}
@Override
public Loader onCreateLoader(int id, Bundle args) {
SpecialLoader loader = new SpecialLoader(MainActivity.this);
return loader;
}
@Override
public void onLoadFinished(Loader loader, Object data) {
adapter.swapCursor((Cursor) data);
}
@Override
public void onLoaderReset(Loader loader) {
}
}
后我們看下運行的效果:
好,那到這里 又有人要說了,你這個說來說去 還不是只能支持provider或者db類型的數據源嗎?好 接著往下,我們給出另外一個例子,不過這個例子是谷歌官方的例子,我取其中重要的部分給予注釋講解。
http://developer.android.com/intl/zh-cn/reference/android/content/AsyncTaskLoader.html
相關推薦

最新發布
性能測試之測試環境搭建的方法
2020/7/21 15:39:32軟件測試是從什么時候開始被企業所重視的呢?
2020/7/17 9:09:11Android自動化測試框架有哪些?有什么用途?
2020/7/17 9:03:50什么樣的項目適合做自動化?自動化測試人員應具備怎樣的能力?
2020/7/17 8:57:06幾大市面主流性能測試工具測評
2020/7/17 8:52:11RPA機器人能夠快速響應企業需求,是怎么做到的?
2020/7/17 8:48:05Bug可以真正消滅嗎?為什么?
2020/7/17 8:43:03軟件測試基本概念是怎么來的?軟件測試生命周期的形成歷經了什么?
2020/7/16 9:11:10