Background Thread(In) -> Perform Network Request -> Out
Main Thread(In) -> Click Button -> Out
Asynctask to the rescue
Async -> 非同期
https://developer.android.com/reference/android/os/AsyncTask.html
private class DownloadFilesTask extends AsyncTask{ protected Long doInBackground(URL... urls) { int count = urls.length; long totalSize = 0; for (int i = 0; i < count; i++) { totalSize += Downloader.downloadFile(urls[i]); publishProgress((int) ((i / (float) count) * 100)); // Escape early if cancel() is called if (isCancelled()) break; } return totalSize; } protected void onProgressUpdate(Integer... progress) { setProgressPercent(progress[0]); } protected void onPostExecute(Long result) { showDialog("Downloaded " + result + " bytes"); } }
concrete class
abstract class
interface
public class Main Activity extends Activity { onCreate(){ } onCreate(){ } } public class EarthquakeAsyncTask extends AsyncTask { doInBackground(){ } }