如何在 Android 中為 AsyncTask 設定超時時間?
本例演示如何在 android 中為 AsyncTask 設定超時時間。
步驟 1 − 在 Android Studio 中新建一個專案,轉至檔案 ⇒ 新建專案並填寫所有必需的詳細資訊以新建一個專案。
步驟 2 − 將以下程式碼新增到 res/layout/activity_main.xml。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" tools:context=".MainActivity"> <EditText android:layout_marginTop="50dp" android:id="@+id/enterTime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:background="@android:drawable/editbox_background" android:inputType="number"/> <Button android:id="@+id/btnRun" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Run Async task" android:layout_below="@+id/enterTime" android:layout_centerHorizontal="true" /> </RelativeLayout>
步驟 3 − 將以下程式碼新增到 src/MainActivity.java
import androidx.appcompat.app.AppCompatActivity; import android.app.ProgressDialog; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { Button button; EditText editTextTime; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = findViewById(R.id.btnRun); editTextTime = findViewById(R.id.enterTime); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AsyncTaskRunner runner = new AsyncTaskRunner(); String sleepTime = editTextTime.getText().toString(); runner.execute(sleepTime); } }); } private class AsyncTaskRunner extends AsyncTask<String, String, String> { ProgressDialog progressDialog; @Override protected String doInBackground(String... params) { publishProgress("AsyncTask started..."); String resp; try { int time = Integer.parseInt(params[0]) * 1000; Thread.sleep(time); resp = "Slept for " + params[0] + " seconds"; } catch (Exception e) { e.printStackTrace(); resp = e.getMessage(); } return resp; } @Override protected void onPostExecute(String result) { progressDialog.dismiss(); Toast.makeText(MainActivity.this, "AsyncTask Terminated", Toast.LENGTH_SHORT).show(); } @Override protected void onPreExecute() { progressDialog = ProgressDialog.show(MainActivity.this, "ProgressDialog", "Wait for " + editTextTime.getText().toString() + " seconds"); } @Override protected void onProgressUpdate(String... text) { Toast.makeText(MainActivity.this, "" + text[0], Toast.LENGTH_SHORT).show(); } } }
步驟 4 − 將以下程式碼新增到 androidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.sample"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
讓我們嘗試執行你的應用程式。我假設你已經將你的實際 Android 移動裝置與你的計算機相連線。要在 android studio 中執行應用,開啟一個你的專案活動檔案,然後點選工具欄中的執行 圖示。選擇你的移動裝置作為選項,然後檢視你的移動裝置,它將顯示預設螢幕 −
點選 這裡 下載專案程式碼。
廣告