如何在 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 中运行应用,打开一个你的项目活动文件,然后点击工具栏中的运行
图标。选择你的移动设备作为选项,然后查看你的移动设备,它将显示默认屏幕 −


点击 这里 下载项目代码。
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP