Android SDK - 在后台运行功能 [英] Android SDK - running functions in the background
问题描述
我有一个功能,可以在完成时间不同。我想在这个函数运行时显示一个进度对话框。
我知道你可以使用'Thread'来实现这个功能。有人可以指出我正确的方向吗?
编辑:
下面是我使用的代码:
private class LongOperation扩展AsyncTask< String,Void,String>
{
ProgressDialog对话框;
公共上下文上下文;
@Override
protected String doInBackground(String ... params){
if(!dialog.isShowing())
dialog.show(); //以防万一
返回null;
$ b $ *(非Javadoc)
* @ android.os.AsyncTask#onPostExecute(java.lang.Object)
* /
@Override
protected void onPostExecute(String result){
dialog.dismiss();
$ b $ *(非Javadoc)
* @ android.os.AsyncTask#onPreExecute()
* /
@Override
保护void onPreExecute()
{
dialog = ProgressDialog.show(context,Working,获取友善信息,true);
$ b $ *(非Javadoc)
@see android.os.AsyncTask#onProgressUpdate(Progress [])
* /
@覆盖
protected void onProgressUpdate(Void ... values){
//执行长时间运行的操作正在进行时要做的事情。例如更新ProgessDialog
}
}
这是Asnyc类。用户从菜单中选择一个选项,然后执行:
longOperation.execute(); //启动异步任务
GetAmenities(Trails.UserLocation); //长函数操作
$ c> AsyncTask 用于此目的。请参阅 Android开发人员网站和如何使用AsyncTask 。
一些示例代码:
private class LongRunningTask extends AsyncTask< Void,Boolean,Boolean> {
私人ProgressDialog进度;
保护void onPreExecute(){
progress = ProgressDialog.show(yourContext,Title,Text);
}
@Override
protected Boolean doInBackground(Void ... params){
return true;
保护无效onPostExecute(布尔结果){
if(result){
progress.dismiss();
}
}
}
I have a function which can vary in the time it takes to finish. I would like to display a progress dialog whilst this function is operating.
I am aware that you can use a 'Thread' to achieve this. Can someone point me in the right direction for doing this ?
EDIT: Here is the code I am using:
private class LongOperation extends AsyncTask<String, Void, String>
{
ProgressDialog dialog;
public Context context;
@Override
protected String doInBackground(String... params) {
if (!dialog.isShowing())
dialog.show(); // Just in case
return null;
}
/* (non-Javadoc)
* @see android.os.AsyncTask#onPostExecute(java.lang.Object)
*/
@Override
protected void onPostExecute(String result) {
dialog.dismiss();
}
/* (non-Javadoc)
* @see android.os.AsyncTask#onPreExecute()
*/
@Override
protected void onPreExecute()
{
dialog = ProgressDialog.show(context, "Working", "Getting amenity information", true);
}
/* (non-Javadoc)
* @see android.os.AsyncTask#onProgressUpdate(Progress[])
*/
@Override
protected void onProgressUpdate(Void... values) {
// Things to be done while execution of long running operation is in progress. For example updating ProgessDialog
}
}
this is the Asnyc class. The user selects an option from the menu, and this is then executed:
longOperation.execute(""); // Start Async Task
GetAmenities(Trails.UserLocation); // Long function operation
You should use AsyncTask
for this purpose. See Android developers website and How to use AsyncTask.
Some sample code:
private class LongRunningTask extends AsyncTask<Void, Boolean, Boolean> {
private ProgressDialog progress;
protected void onPreExecute() {
progress = ProgressDialog.show(yourContext, "Title", "Text");
}
@Override
protected Boolean doInBackground(Void... params) {
return true;
}
protected void onPostExecute(Boolean result) {
if(result) {
progress.dismiss();
}
}
}
这篇关于Android SDK - 在后台运行功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!