Android 异步任务和进度条计时
在对话框中按ok之后,我应该从其他地方获取数据。所以我使用了Asynctask类。实施情况如下。但是在按下第一个Ok后,我不会得到任何进度条。我只在按下第二个ok(基本上,当ButtonOnClick方法中的所有行都被执行时)之后得到它。。。 在按“第一个确定”后,我应该做些什么来获得正确的进度条Android 异步任务和进度条计时,android,Android,在对话框中按ok之后,我应该从其他地方获取数据。所以我使用了Asynctask类。实施情况如下。但是在按下第一个Ok后,我不会得到任何进度条。我只在按下第二个ok(基本上,当ButtonOnClick方法中的所有行都被执行时)之后得到它。。。 在按“第一个确定”后,我应该做些什么来获得正确的进度条 confirmPath= (Button) findViewById(R.id.confirmPath); confirmPath.setOnClickListener(new
confirmPath= (Button) findViewById(R.id.confirmPath);
confirmPath.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
construction
AlertDialog.Builder builder = new AlertDialog.Builder(Destination.this);
builder.setMessage("Press 'Ok' ")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
LoadData task = new LoadData();
task.execute();
AlertDialog alertDialog = new AlertDialog.Builder(Destination.this).create();
alertDialog.setTitle("Attention!");
alertDialog.setMessage(" Pay attention");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do nothing
}
});
builder.create();
builder.show();
这是AsynkTask类:
public class LoadData extends AsyncTask<Void, Void, Void> {
ProgressDialog progressDialog;
//declare other objects as per your need
@Override
protected void onPreExecute()
{
progressDialog= ProgressDialog.show(Destination.this, "Progress Dialog Title Text","Please wait", true);
//do initialization of required objects objects here
};
@Override
protected Void doInBackground(Void... params)
{
ReadFromFile readFromFile= new ReadFromFile();
readFromFile.ReadAllData("Data");
//some other tasks to do
return null;
}
@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
progressDialog.dismiss();
};
}
公共类LoadData扩展异步任务{
进行对话进行对话;
//根据需要声明其他对象
@凌驾
受保护的void onPreExecute()
{
progressDialog=progressDialog.show(Destination.this,“进度对话框标题文本”,“请稍候”,true);
//在此处执行所需对象的初始化
};
@凌驾
受保护的Void doInBackground(Void…参数)
{
ReadFromFile ReadFromFile=新建ReadFromFile();
readFromFile.ReadAllData(“数据”);
//还有一些其他任务要做
返回null;
}
@凌驾
受保护的void onPostExecute(void结果)
{
super.onPostExecute(结果);
progressDialog.disclose();
};
}
将此代码放在AlertDialogBuilder之外:
LoadData task = new LoadData();
task.execute();
@Override
public void onClick(View arg0) {
LoadData task = new LoadData();
task.execute();
construction
AlertDialog.Builder builder = new AlertDialog.Builder(Destination.this);
builder.setMessage("Press 'Ok' ")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
AlertDialog alertDialog = new AlertDialog.Builder(Destination.this).create();
alertDialog.setTitle("Attention!");
alertDialog.setMessage(" Pay attention");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do nothing
}
});
builder.create();
builder.show();
新代码应该如下所示:
LoadData task = new LoadData();
task.execute();
@Override
public void onClick(View arg0) {
LoadData task = new LoadData();
task.execute();
construction
AlertDialog.Builder builder = new AlertDialog.Builder(Destination.this);
builder.setMessage("Press 'Ok' ")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
AlertDialog alertDialog = new AlertDialog.Builder(Destination.this).create();
alertDialog.setTitle("Attention!");
alertDialog.setMessage(" Pay attention");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do nothing
}
});
builder.create();
builder.show();
使用此代码
private class NetworkDelegate extends AsyncTask<String, Void, String> {
private ProgressDialog Dialog = new ProgressDialog(MainActivity.this);
protected String doInBackground(String... pass) {
try {
//userid is getting from webservice
return userid;
} catch (Exception e) {
System.out.println(e);
}
}
protected void onPostExecute(String result) {
Dialog.dismiss();
if (result == null) {
tv.setText("Enter Your Password Correctly");
pwet.setFocusable(true);
} else {
}
}
protected void onPreExecute() {
Dialog.setMessage("Loading...");
Dialog.show();
}
}
私有类NetworkDelegate扩展异步任务{
private ProgressDialog=新建ProgressDialog(MainActivity.this);
受保护的字符串背景(字符串…通过){
试一试{
//用户ID正在从webservice获取
返回用户标识;
}捕获(例外e){
系统输出打印ln(e);
}
}
受保护的void onPostExecute(字符串结果){
Dialog.dismise();
如果(结果==null){
tv.setText(“正确输入密码”);
pwet.setFocusable(真);
}否则{
}
}
受保护的void onPreExecute(){
setMessage(“加载…”);
Dialog.show();
}
}
我应该将第二个alertdialog也放在asynctaskonPostExecute
方法中。然后我之前有progressbar,显然是但是我需要在我按下alertdialog的第一个ok后发送数据。但是现在,根据我们的解决方案,我会在按下confirmPath后得到它,这是一个步骤之前..这是错误的删除alert对话框。我不明白为什么在单击按钮后需要对话框。我无法删除它。这是我们应该给用户的警告消息的一部分。请参阅回复过程,在警告消息之前无法启动。您需要单击“确定”在警告按钮中继续后台进程。否,progressbar应该在最后一个对话框之前可见。之前的问题是我总是在之后看到它。我应该将最后一个对话框也放在asynktask中