我是一名 android 开发初学者,所以我想让 2 个线程在后台同时运行(或不同时运行),一个用于测试 Wifi 连接,另一个用于超时响应进度条(或另一个用于显示应用程序没有死的东西),在“main”中我要等待,如果 wifi 测试比超时快,则发送用户一页,如果超时用完,则发送用户另一页。我已经有线程,一个做超时+进度条和其他检查连接,我的问题是谁在等待快速线程结束,我尝试循环检查thread.isalive(),但这样进度条没有t 显示,并且应用程序冻结,直到某个线程结束... =\
有什么帮助吗?!对不起我的英语不好...
公共无效 startProgress() {
Runnable r1 = new Runnable() {
public void run() {
for (int i = 0; i <= 10; i++) {
final int value = i;
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
public void run() {
progress.setProgress(value);
}
});
}
setContentView(R.layout.error_menu);
//finish();
}
};
Thread th1 = new Thread(r1);
th1.start();
Runnable r2 = new Runnable() {
// Setup the run() method that is called when the background thread
// is started.
public void run() {
// Do you background thread process here...
// Checking if WIFI is ON and IF URL is Reachable
if(isOnline()){
String URL_STR="http://10.0.1.2/ShopList_for_app.php";
try
{
URL url = new URL(URL_STR);
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(7000); // This is time limit if the connection time limit
urlc.connect();
if (urlc.getResponseCode() == 200){
setContentView(R.layout.menu1);
//finish();//return;
}else{
//finish();//return;
}
}
catch (MalformedURLException e){
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
//finish();//return;
}
catch (IOException e){
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
//finish();//return;
}
}else{
//finish();//return;
}
}
};
Thread th2 = new Thread(r2);
th2.start();
Toast.makeText(getBaseContext(), "..Correu todas as Threads..", Toast.LENGTH_LONG).show();
while(true){
if(th2.isAlive()){
//Toast.makeText(getBaseContext(),"checking...", Toast.LENGTH_LONG).show();
}else{
th1.interrupt();
setContentView(R.layout.menu1);
break;
}
if(th1.isAlive()){
//Toast.makeText(getBaseContext(),"checking...", Toast.LENGTH_LONG).show();
}else{
th2.interrupt();
setContentView(R.layout.error_menu);
break;
}
}
}