[英]How to check My test android device is connected to internet or not?
protected boolean isOnline()
{
ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info != null && info.isConnectedOrConnecting()) {
return true;
}else
{
return false;
}
}
public void requestData()
{
if (isOnline())
{
Toast.makeText(getApplicationContext(), "connected", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "Niot Connected", Toast.LENGTH_LONG).show();
}
}
I have written this code but I am getting An error something like "Unfortunately your program has stopped".. Please help me.. Thanks In Advance 我已经编写了这段代码,但是出现了类似“不幸的是您的程序已停止”之类的错误。.请帮助我..提前谢谢
您需要将此添加到您的androidmanifest.xml
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Please Check This Link http://www.androidhive.info/2012/07/android-detect-internet-connection-status/ 请检查此链接http://www.androidhive.info/2012/07/android-detect-internet-connection-status/
its the Demo of Android Detect Internet Connection Status Android的演示检测互联网连接状态
To access internet we need INTERNET Permission 要访问互联网,我们需要INTERNET许可
To detect network status we need ACCESS_NETWORK_STATE Permission 要检测网络状态,我们需要ACCESS_NETWORK_STATE权限
Call this method 调用此方法
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
and declare permission 并宣布允许
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Here's the code that works fine for me. 这是适合我的代码。 Define this class in your utils. 在您的utils中定义此类。
public class InternetConnection {
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager
= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
}
Then, inside the activity where you want to check the connection status, use, 然后,在要检查连接状态的活动中,使用,
if(!(InternetConnection.isNetworkAvailable(this))){
internetActive = false;
Toast.makeText(this, "Please try again with active Internet connection", Toast.LENGTH_LONG).show();
}
else {
// your code if device is connected to internet
}
Make sure you have 确保你有
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
permission in your manifest. 您清单中的权限。
you will check both WIFI and Mobile data connected or not by using the following two methods: 您将使用以下两种方法检查是否已连接WIFI和移动数据:
public boolean checkWIFIInternetConn() {
//Create object for ConnectivityManager class which returns network related info
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
//If connectivity object is not null
if (connectivity != null) {
//Get network info - WIFI internet access
NetworkInfo info = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (info != null) {
//Look for whether device is currently connected to WIFI network
if (info.isConnected()) {
return true;
}
}
}
return false;
}
public boolean checkMobileInternetConn() {
//Create object for ConnectivityManager class which returns network related info
ConnectivityManager connectivity = (ConnectivityManager) _context
.getSystemService(Context.CONNECTIVITY_SERVICE);
//If connectivity object is not null
if (connectivity != null) {
//Get network info - Mobile internet access
NetworkInfo info = connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (info != null) {
//Look for whether device is currently connected to Mobile internet
if (info.isConnected()) {
return true;
}
}
}
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.