Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android LoaderCallbacks

Android LoaderCallbacks

編輯:關於Android編程

在Fragment裡面使用LoaderCallbacks接口 異步HTTP獲取圖片

public abstract class AbstractLoader extends AsyncTaskLoader {

    public AbstractLoader(Context context) {
        super(context);
    }

    @Override
    protected void onStartLoading() { //會自動調用  step2
        super.onStartLoading();
        forceLoad();//開始執行任務
    }
}


public class BitmapLoader extends AbstractLoader {

    static final String IMG_URL = "http://h.hiphotos.baidu.com/image/pic/item/dcc451da81cb39db599abb5bd2160924ab183061.jpg";

    public BitmapLoader(Context context) {
        super(context);
    }

    @Override
    public Bitmap loadInBackground() { //後台執行任務 step3
        InputStream ins = HTTPHelper.httpGet(IMG_URL);
        return BitmapFactory.decodeStream(ins);
    }

}

public final class HTTPHelper {

    public HTTPHelper() {
    }

    public static InputStream httpGet(String url) {
        HttpURLConnection connection = (HttpURLConnection) createConnection(url, 5000);
        try {
            if (connection != null && connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                return connection.getInputStream();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static URLConnection createConnection(String url, int timeout) {
        try {
            URLConnection urlConnection = (URLConnection) new URL(url).openConnection();
            urlConnection.setConnectTimeout(timeout);
            urlConnection.setReadTimeout(timeout);
            urlConnection.setDoInput(true);
            return urlConnection;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}

在Frament中使用


public class MyFragment extends Fragment implements LoaderCallbacks{

    static final String TAG = "MyFragment";
    private ImageView imageView;

    public MyFragment() {
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_img_layout, null);
    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.d(TAG,"onViewCreated>>>" + getActivity());
        imageView = (ImageView) view.findViewById(R.id.img);
        //初始化任務執行 step1
        getLoaderManager().initLoader(0, null, this);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override //創建異步加載Loader
    public Loader onCreateLoader(int arg0, Bundle b) {
        return new BitmapLoader(getActivity());
    }

    @Override //任務執行完成後 直接更新UI  step4
    public void onLoadFinished(Loader arg0, Bitmap bitmap) {
        if (bitmap != null) {
            imageView.setImageBitmap(bitmap);
        }
    }

    @Override
    public void onLoaderReset(Loader loader) {

    }

}



public class MainActivity extends FragmentActivity {

    static String TAG = MainActivity.class.getCanonicalName();

    public MainActivity() {
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportFragmentManager().beginTransaction().replace(R.id.container, new MyFragment()).addToBackStack(null).commit();



    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }

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