Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android資訊 >> Android LoaderManager 和 Loader 初步使用

Android LoaderManager 和 Loader 初步使用

編輯:Android資訊

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 >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 > </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的用法,顯示數據

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved