检测android设备是否已连接到互联网

编程入门 行业动态 更新时间:2024-10-23 09:34:50
本文介绍了检测android设备是否已连接到互联网的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我的课程,检查设备是否已连接到互联网。

this is my class that checks if the device is connected to the internet.

import android.app.Activity; import android.content.Context; import android.ConnectivityManager; import android.NetworkInfo; import android.util.Log; import java.io.IOException; import java.HttpURLConnection; import java.URL; public class ConnectionDetector { private Context _context; public ConnectionDetector(Context context) { this._context = context; } public boolean isConnectingToInternet() { if (networkConnectivity()) { try { HttpURLConnection urlc = (HttpURLConnection) (new URL( "www.google").openConnection()); urlc.setRequestProperty("User-Agent", "Test"); urlc.setRequestProperty("Connection", "close"); urlc.setConnectTimeout(3000); urlc.setReadTimeout(4000); urlc.connect(); // networkcode2 = urlc.getResponseCode(); return (urlc.getResponseCode() == 200); } catch (IOException e) { return (false); } } else return false; } private boolean networkConnectivity() { ConnectivityManager cm = (ConnectivityManager) _context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = cm.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnected()) { return true; } return false; } }

然后我在这里称呼它似乎没有返回true或false。没有错误,我的应用程序不会崩溃,只是不打印任何内容。有人知道为什么吗?

then i call it here but it doesnt seem to return true or false. there are no errors and my app doesnt crash it just doesnt print anything out. anyone know why?

public void CheckInternet(){ // Boolean isInternetPresent; ConnectionDetector cd = new ConnectionDetector(getApplicationContext()); // isInternetPresent = cd.isConnectingToInternet(); if (cd.isConnectingToInternet()) { // Internet Connection is Present Log.i(TAG, "INTERNET IS GUUD"); } else { // Internet connection is not present // Ask user to connect to Internet Log.i(TAG, "INTERNET IS NOOOO GUUD"); } }

推荐答案

创建一个类:

public class Utility { public static boolean isNetworkAvailable(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); return activeNetworkInfo != null && activeNetworkInfo.isConnected(); } }

然后您从活动中调用methode,它将返回true或否:

Then you call methode from activity, it will return true or false:

Utility.isNetworkAvailable(AnyActivity.this);

别忘了向Android清单添加权限

And don't forget to add permission to android manifest

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.INTERNET"></uses-permission>

更多推荐

检测android设备是否已连接到互联网

本文发布于:2023-11-27 20:11:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639436.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:互联网   连接到   设备   android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!