刚刚接触线程
自己写的一个小程序
显示进度条对话框时对话框只显示一下就没有了
而且程序也不往下执行了
希望有人帮我分析一下代码如下:
try {
// 用线程启动进度条
viewOrders = new Runnable() {
@Override
public void run() {
dosomething();
}
};
Thread thread = new Thread(null, viewOrders, "Background");
thread.start();
myDialog = ProgressDialog.show(Main.this, "登录", "正在登录,请稍后...",
true);
} catch (Exception e) {
e.printStackTrace();
}
// 线程运行时操作
private void dosomething() {
try { // 获取文本框的内容
Name = nameText.getText().toString();
Pass = passText.getText().toString(); if (!"".equals(Name) && Name != null ) {
// http地址
String httpUrl = "http://192.168.1.XXX";
resultData = "";
URL url = null;
try {
url = new URL(httpUrl);
HttpURLConnection urlConn = (HttpURLConnection) url
.openConnection();
inStream = urlConn.getInputStream(); if (urlConn.getResponseCode() == 200) { InputStreamReader in = new InputStreamReader(inStream); // 为输出创建BufferedReader
BufferedReader buffer = new BufferedReader(in);
String inputLine = null;
// 使用循环来读取获得的数据
while (((inputLine = buffer.readLine()) != null)) {
// 我们在每一行后面加上一个"\n"来换行
resultData += inputLine;
}
// 关闭InputStreamReader
in.close();
Toast.makeText(getApplication(), "登录成功",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Main.this,
Contact_list.class);
intent.putExtra("contactList", list);
startActivity(intent);
Main.this.finish();
// 关闭http连接
urlConn.disconnect();
}
}
} catch (ClientProtocolException e) {
Toast.makeText(getApplication(), e.toString(),
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(getApplication(), "访问服务器失败请重试",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getApplication(), e.toString(),
Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
runOnUiThread(returnRes);
} private Runnable returnRes = new Runnable() {
@Override
public void run() {
myDialog.dismiss();
// 执行成功后
}
};
自己写的一个小程序
显示进度条对话框时对话框只显示一下就没有了
而且程序也不往下执行了
希望有人帮我分析一下代码如下:
try {
// 用线程启动进度条
viewOrders = new Runnable() {
@Override
public void run() {
dosomething();
}
};
Thread thread = new Thread(null, viewOrders, "Background");
thread.start();
myDialog = ProgressDialog.show(Main.this, "登录", "正在登录,请稍后...",
true);
} catch (Exception e) {
e.printStackTrace();
}
// 线程运行时操作
private void dosomething() {
try { // 获取文本框的内容
Name = nameText.getText().toString();
Pass = passText.getText().toString(); if (!"".equals(Name) && Name != null ) {
// http地址
String httpUrl = "http://192.168.1.XXX";
resultData = "";
URL url = null;
try {
url = new URL(httpUrl);
HttpURLConnection urlConn = (HttpURLConnection) url
.openConnection();
inStream = urlConn.getInputStream(); if (urlConn.getResponseCode() == 200) { InputStreamReader in = new InputStreamReader(inStream); // 为输出创建BufferedReader
BufferedReader buffer = new BufferedReader(in);
String inputLine = null;
// 使用循环来读取获得的数据
while (((inputLine = buffer.readLine()) != null)) {
// 我们在每一行后面加上一个"\n"来换行
resultData += inputLine;
}
// 关闭InputStreamReader
in.close();
Toast.makeText(getApplication(), "登录成功",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Main.this,
Contact_list.class);
intent.putExtra("contactList", list);
startActivity(intent);
Main.this.finish();
// 关闭http连接
urlConn.disconnect();
}
}
} catch (ClientProtocolException e) {
Toast.makeText(getApplication(), e.toString(),
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(getApplication(), "访问服务器失败请重试",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getApplication(), e.toString(),
Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
runOnUiThread(returnRes);
} private Runnable returnRes = new Runnable() {
@Override
public void run() {
myDialog.dismiss();
// 执行成功后
}
};
public void onClick(View view){
progressDialog = ProgressDialog.show(DialogTest.this, "等待框", "正在运行中...");
//定义消息处理器来处理消息
progresshandler = new Handler(new Handler.Callback(){
public boolean handleMessage(Message msg) {
String message = msg.obj.toString();
progressDialog.setMessage(message);
return true;
}
}); //倒计时线程
Thread thread = new Thread(){
@Override
public void run() {
try {
int second = 10;
while(second>0){
String message = String.format("还需%d秒...", second);
//progressDialog.setMessage(message); //不能跨线程操作View!!!
//发送进度消息
Message msg = new Message();
msg.obj = message;
progresshandler.sendMessage(msg);
sleep(1*1000);
second--;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
progressDialog.dismiss();
}
};
thread.start();
}
后台运行的动作在哪些 还要实现两个activity之间的跳转
Handel 里实现跳转 。 出现异常传送Message