【发布时间】:2018-10-25 14:43:10
【问题描述】:
当我按下登录按钮时,应用程序会冻结一段时间,因为它会检查主线程上的互联网连接。如何在后台线程或新线程中执行,它们之间有什么区别(后台线程和另一个新线程)。
我的上网查询功能:
public Boolean isOnline() {
Runtime runtime = Runtime.getRuntime();
try {
Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
int exitValue = ipProcess.waitFor();
return (exitValue == 0);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return false;
}
【问题讨论】:
-
您要检查互联网连接是否打开了用户数据?或者数据是否真的在流动?
-
the app freezes for a while because it checks internet connection on the main thread。只能从与主线程不同的线程检查 Internet。由于ipProcess.waitFor();,您的应用程序冻结 -
你最好把所有的东西都放在一个线程中,如果线程完成了你启用登录按钮。
标签: android multithreading android-studio