Android-单击按钮后立即显示进度对话框
我正在做懒加载,几乎完成了。但是我想用它实现一个进度对话框,因为从开始活动到完成显示内容大约需要10秒。一旦我点击一个按钮开始,它会在当前pageMain.java停留大约4秒钟,然后再移动到下一个pageActivity.java。然后显示内容大约需要2-4秒 尝试了这里和网上提供的示例,但效果不好,无法显示对话框,但无法在下载完所有内容后进行适当的删除 问题是,一旦用户单击按钮,我如何立即实现进度指示器 Activity.javaAndroid-单击按钮后立即显示进度对话框,android,progressdialog,Android,Progressdialog,我正在做懒加载,几乎完成了。但是我想用它实现一个进度对话框,因为从开始活动到完成显示内容大约需要10秒。一旦我点击一个按钮开始,它会在当前pageMain.java停留大约4秒钟,然后再移动到下一个pageActivity.java。然后显示内容大约需要2-4秒 尝试了这里和网上提供的示例,但效果不好,无法显示对话框,但无法在下载完所有内容后进行适当的删除 问题是,一旦用户单击按钮,我如何立即实现进度指示器 Activity.java public void onCreate(Bundle sa
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list=(ListView)findViewById(R.id.list);
adapter=new LazyAdapter(this, mStrings, dStrings );
list.setAdapter(adapter);
}
private String[] mStrings = {};
private String[] dStrings = {};
public Activity()
{
String imageC = "";
String textC = "";
try {
// Get the URL from text box and make the URL object
URL url = new URL(targetURL);
// Make the connection
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line = reader.readLine();
Pattern sChar = Pattern.compile("&.*?;");
Matcher msChar = sChar.matcher(line);
while (msChar.find()) line = msChar.replaceAll("");
while (line != null) {
if(line.contains("../../"))
{
int startIndex = line.indexOf("../../") + 6;
int endIndex = line.indexOf(">", startIndex + 1);
String abc = "http://www.petschannel.com/";
String imageSrc = line.substring(startIndex,endIndex);
//complete full url
String xyz = abc +imageSrc;
xyz = xyz.substring(0,xyz.indexOf('"'));
xyz = xyz +";";
imageC += xyz;
mStrings = imageC.split(";");
line = reader.readLine();
}
if(line.contains("../../") == false)
{
line = reader.readLine();
}
if (line.contains("Gnametag"))
{
int startIndex = line.indexOf("Gnametag") + 10;
int endIndex = line.indexOf("<", startIndex + 1);
String gname = line.substring(startIndex,endIndex);
textC += "Name: "+gname+ "\n";
}
if (line.contains("Last Update"))
{
reader.close();
}
}
// Close the reader
reader.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
大概是这样的:
final ProgressDialog myDialog = ProgressDialog.show(this, "Title", "Message");
Thread thread = new Thread(new Runnable() {
public void run() {
/* Your code goes here */
mHandler.sendEmptyMessage(0);
}
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
myDialog.dismiss();
}
}
}
thread.start();
首先,您在主线程上进行网络调用,这是一个典型的基于性能原因的拒绝。切勿在主ui线程上执行任何可能耗时的操作 我建议使用,这可以确保在本例中,您的网络调用将在工作线程中完成,并将结果发布回主线程
AsyncTask具有管理进度条、onProgressUpdate和publishProgress的方法,这些方法将帮助您解决所述问题。有很多关于这方面的好文章。那么对我的案例有什么建议呢?我尝试创建一个私有字符串[]。然后将方法更改为void。但它没有运行这个方法。使用AsyncTask我需要做什么?在里面调用我的方法?谢谢