[embedyt] https://www.youtube.com/watch?v=uKx0FuVriqA[/embedyt]
activity_main.xmlMainActivity.java
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" tools:context="com.codinginflow.asynctaskexample.MainActivity"> <ProgressBar android:id="@+id/progress_bar" style="@style/Widget.AppCompat.ProgressBar.Horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="16dp" android:visibility="invisible" tools:visibility="visible" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="startAsyncTask" android:text="Start" /> </LinearLayout>
package com.codinginflow.asynctaskexample; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ProgressBar; import android.widget.Toast; import java.lang.ref.WeakReference; public class MainActivity extends AppCompatActivity { private ProgressBar progressBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progressBar = findViewById(R.id.progress_bar); } public void startAsyncTask(View v) { ExampleAsyncTask task = new ExampleAsyncTask(this); task.execute(10); } private static class ExampleAsyncTask extends AsyncTask<Integer, Integer, String> { private WeakReference<MainActivity> activityWeakReference; ExampleAsyncTask(MainActivity activity) { activityWeakReference = new WeakReference<MainActivity>(activity); } @Override protected void onPreExecute() { super.onPreExecute(); MainActivity activity = activityWeakReference.get(); if (activity == null || activity.isFinishing()) { return; } activity.progressBar.setVisibility(View.VISIBLE); } @Override protected String doInBackground(Integer... integers) { for (int i = 0; i < integers[0]; i++) { publishProgress((i * 100) / integers[0]); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } return "Finished!"; } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); MainActivity activity = activityWeakReference.get(); if (activity == null || activity.isFinishing()) { return; } activity.progressBar.setProgress(values[0]); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); MainActivity activity = activityWeakReference.get(); if (activity == null || activity.isFinishing()) { return; } Toast.makeText(activity, s, Toast.LENGTH_SHORT).show(); activity.progressBar.setProgress(0); activity.progressBar.setVisibility(View.INVISIBLE); } } }