Android 已连接网络上的活动Internet连接
我的应用程序活动有一段代码,在访问FireBase Auth Login之前,我想检查连接的网络是否具有活动连接 我为networkState创建了一个类,并添加了一块用于检查networkActiveConnection的代码Android 已连接网络上的活动Internet连接,android,android-studio,networking,wifi,network-state,Android,Android Studio,Networking,Wifi,Network State,我的应用程序活动有一段代码,在访问FireBase Auth Login之前,我想检查连接的网络是否具有活动连接 我为networkState创建了一个类,并添加了一块用于检查networkActiveConnection的代码 应用程序不断崩溃。没有解决方案我无法行动。我错过了什么?是否有其他方法来检查连接的网络是否有活动连接?最后我找到了答案。这实际上是由于版本的原因。在Android 3.0及以上版本中,所有长流程活动都只能在AsyncTask上工作 我通过Google.com的负载检查重
应用程序不断崩溃。没有解决方案我无法行动。我错过了什么?是否有其他方法来检查连接的网络是否有活动连接?最后我找到了答案。这实际上是由于版本的原因。在Android 3.0及以上版本中,所有长流程活动都只能在AsyncTask上工作 我通过Google.com的负载检查重新构建了设备中的实际互联网连接。我不知道google.com关闭后会发生什么 下面的代码可能会有所帮助
@SuppressLint("StaticFieldLeak")
public class activeConnection extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
try {
URL url = new URL("http://www.google.com");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setConnectTimeout(3000);
urlc.connect();
if (urlc.getResponseCode() == 200) {
return true;
}
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return false;
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
protected void onPostExecute(Boolean result) {
if (!result) { // code if not connected
AlertDialog.Builder builder = new AlertDialog.Builder(Customers.this, R.style.MyDialogTheme);
builder.setTitle("ALERT");
builder.setMessage("Activate your Internet connection and Try again");
builder.setCancelable(false);
builder.setPositiveButton(
"Retry",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
new activeConnection().execute();
}
});
AlertDialog alert11 = builder.create();
alert11.show();
} else { // code if connected
}
}
}
在搜索hourI验证此答案并修改我的代码后,发现此答案可能重复。请修改我的密码
@SuppressLint("StaticFieldLeak")
public class activeConnection extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
try {
URL url = new URL("http://www.google.com");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setConnectTimeout(3000);
urlc.connect();
if (urlc.getResponseCode() == 200) {
return true;
}
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return false;
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
protected void onPostExecute(Boolean result) {
if (!result) { // code if not connected
AlertDialog.Builder builder = new AlertDialog.Builder(Customers.this, R.style.MyDialogTheme);
builder.setTitle("ALERT");
builder.setMessage("Activate your Internet connection and Try again");
builder.setCancelable(false);
builder.setPositiveButton(
"Retry",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
new activeConnection().execute();
}
});
AlertDialog alert11 = builder.create();
alert11.show();
} else { // code if connected
}
}
}