已连接网络上的活动 Internet 连接
Active Internet Connection on connected network
我的应用程序 activity 有一段代码,我想在访问 FireBase 身份验证登录之前检查连接的网络是否具有活动连接。
我为 networkState 创建了一个 class 添加了一个代码块来检查 networkActiveConnection
private void networkState() throws IOException {
final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
assert conMgr != null;
final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnectedOrConnecting()) {
Toast.makeText(getApplicationContext(),"Network connected",Toast.LENGTH_SHORT).show();
//checking active internet service
HttpURLConnection urlc = (HttpURLConnection)(new URL("http://www.google.com").openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(10000);
urlc.connect();
if(urlc.getResponseCode() == 200){
Toast.makeText(getApplicationContext(), "Network has active internet", Toast.LENGTH_SHORT).show();
//user login
signInUser();
}else {
Toast.makeText(getApplicationContext(), "No active internet connection", Toast.LENGTH_SHORT).show();
}
//end of checking active internet service
} else {
Toast.makeText(getApplicationContext(),"Network not connected",Toast.LENGTH_SHORT).show();
}
}
应用程序不断崩溃。没有我错过的 solution.Where 我就不能动了?是否有任何其他方法来检查连接的网络是否具有活动连接?
最后我发现 answer.Its 由于版本的原因实际发生了。直到 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
}
}
}
我的应用程序 activity 有一段代码,我想在访问 FireBase 身份验证登录之前检查连接的网络是否具有活动连接。
我为 networkState 创建了一个 class 添加了一个代码块来检查 networkActiveConnection
private void networkState() throws IOException {
final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
assert conMgr != null;
final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnectedOrConnecting()) {
Toast.makeText(getApplicationContext(),"Network connected",Toast.LENGTH_SHORT).show();
//checking active internet service
HttpURLConnection urlc = (HttpURLConnection)(new URL("http://www.google.com").openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(10000);
urlc.connect();
if(urlc.getResponseCode() == 200){
Toast.makeText(getApplicationContext(), "Network has active internet", Toast.LENGTH_SHORT).show();
//user login
signInUser();
}else {
Toast.makeText(getApplicationContext(), "No active internet connection", Toast.LENGTH_SHORT).show();
}
//end of checking active internet service
} else {
Toast.makeText(getApplicationContext(),"Network not connected",Toast.LENGTH_SHORT).show();
}
}
应用程序不断崩溃。没有我错过的 solution.Where 我就不能动了?是否有任何其他方法来检查连接的网络是否具有活动连接?
最后我发现 answer.Its 由于版本的原因实际发生了。直到 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
}
}
}