Java 检查android(eclipse)上的internet连接
我需要android Eclipse编程中此算法的代码:Java 检查android(eclipse)上的internet连接,java,android,eclipse,Java,Android,Eclipse,我需要android Eclipse编程中此算法的代码: If internet connection = connect then open program else show error_activity.xml 尝试此操作以检查internet连接 public void checkNetwork() { ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNEC
If internet connection = connect then open program
else show error_activity.xml
尝试此操作以检查internet连接
public void checkNetwork() {
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
//true : your code
} else {
//false : your code
Log.v("Network Error", "No network connection available.");
}
}
创建一个类ConnectionDetector,并将以下代码放入该类中:
private Context _context;
public ConnectionDetector(Context context) {
this._context = context;
}
public boolean isConnectingToInternet() {
ConnectivityManager connectivity = (ConnectivityManager) _context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
return false;
}
//立即创建
_cd = new ConnectionDetector(getApplicationContext());
_isInternetPresent = _cd.isConnectingToInternet();
检查如下:
if (_isInternetPresent) {
// do you work here
} else {
// no internet,please try again.
}
由于您是一名新生,我将为您提供完整的代码。请尝试了解每一步,以及此代码的工作原理。非常简单,只需创建检测连接的类即可
public class ConnectionDetector {
private Context _context;
public ConnectionDetector(Context context){
this._context = context;
}
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
第1步。创建ConnectionDetector.java
package com.YOURPACKAGE.YOURAPP;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class ConnectionDetector {
private Context _context;
public ConnectionDetector(Context context) {
this._context = context;
}
public boolean checkInternetConn() {
ConnectivityManager connectivity = (ConnectivityManager) _context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo info = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(info == null){
connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
}
if(info == null){
connectivity.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
}
if (info != null) {
if (info.isConnected()) {
return true;
}
}
}
return false;
}
}
第2步。在活动中实施连接检测器
public class YOURACTIVITY extends Activity {
ConnectionDetector cd;
private void methodThatNeedsTheInternet() {
Boolean isConnectionExist = false;
isConnectionExist = cd.checkInternetConn();
// check for Internet status before proceeding
if (!isConnectionExist) {
//no internet, show alert or something
return;
}
//internet works, perform method that needs it
}
}
在提问之前,请进行最少量的研究。另外,StackOverflow并不是为了请求人们为您编写代码;这是为了帮你写代码。对所有的选民来说,我要求删除,他是个新手。
package com.YOURPACKAGE.YOURAPP;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class ConnectionDetector {
private Context _context;
public ConnectionDetector(Context context) {
this._context = context;
}
public boolean checkInternetConn() {
ConnectivityManager connectivity = (ConnectivityManager) _context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo info = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(info == null){
connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
}
if(info == null){
connectivity.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
}
if (info != null) {
if (info.isConnected()) {
return true;
}
}
}
return false;
}
}
public class YOURACTIVITY extends Activity {
ConnectionDetector cd;
private void methodThatNeedsTheInternet() {
Boolean isConnectionExist = false;
isConnectionExist = cd.checkInternetConn();
// check for Internet status before proceeding
if (!isConnectionExist) {
//no internet, show alert or something
return;
}
//internet works, perform method that needs it
}
}