Back to project page AndroidAsyncTaskLoaderSample.
The source code is released under:
MIT License
If you think the Android project AndroidAsyncTaskLoaderSample listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.tatuas.android.asynctaskloadersample; /*from ww w . j a va 2 s. co m*/ import com.example.asynctaskloadersample.R; import android.support.v4.app.LoaderManager; import android.support.v4.app.LoaderManager.LoaderCallbacks; import android.support.v4.content.Loader; import android.support.v4.app.FragmentActivity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ProgressBar; import android.widget.Toast; public class MainActivity extends FragmentActivity implements LoaderCallbacks<String> { public static final int LOADER_ID1 = 1; public static final int LOADER_ID2 = 2; public static final int LOADER_ID3 = 3; public static boolean choiceCountLoader = false; public static boolean isPausing = false; public ProgressBar bar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LoaderManager.enableDebugLogging(true); if (getSupportLoaderManager().getLoader(LOADER_ID1) == null) { Toast.makeText(this, "start load1", Toast.LENGTH_LONG).show(); } getSupportLoaderManager().initLoader(LOADER_ID1, null, this); bar = (ProgressBar) findViewById(R.id.progressBar1); bar.setMax(10); Button btn1 = (Button) findViewById(R.id.button1); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { getSupportLoaderManager().getLoader(LOADER_ID1).stopLoading(); Toast.makeText(v.getContext(), "stop load1", Toast.LENGTH_LONG) .show(); } }); Button btn2 = (Button) findViewById(R.id.button2); btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Bundle args1 = new Bundle(); args1.putString("key", "load2"); getSupportLoaderManager().initLoader( LOADER_ID2, args1, (LoaderCallbacks<String>) ((FragmentActivity) v .getContext())); Toast.makeText(v.getContext(), "start load2", Toast.LENGTH_LONG) .show(); } }); Button btn3 = (Button) findViewById(R.id.button3); btn3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { bar.setProgress(0); Bundle args1 = new Bundle(); args1.putString("key", "load3"); choiceCountLoader = true; getSupportLoaderManager().initLoader( LOADER_ID3, args1, (LoaderCallbacks<String>) ((FragmentActivity) v .getContext())); choiceCountLoader = false; Toast.makeText(v.getContext(), "start load3", Toast.LENGTH_LONG) .show(); } }); Button btn4 = (Button) findViewById(R.id.button4); btn4.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Loader<Object> loader = getSupportLoaderManager().getLoader( LOADER_ID3); if (loader != null) { loader.stopLoading(); loader.reset(); } getSupportLoaderManager().destroyLoader(LOADER_ID3); Toast.makeText(v.getContext(), "stop load3", Toast.LENGTH_LONG) .show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public Loader<String> onCreateLoader(int id, Bundle bundle) { String data = "non data "; if (bundle != null) { data = bundle.getString("key"); } if (choiceCountLoader) { return new MyProgressCountLoader(this, data); } else { return new MyLoader(this, data); } } @Override public void onLoadFinished(Loader<String> loader, String result) { Toast.makeText(this, result, Toast.LENGTH_LONG).show(); } @Override public void onLoaderReset(Loader<String> loader) { // add anything } }