网络应用

​[html]

view plain

copy

print

?

  1. public static boolean isNetworkAvailable(Context context) {
  2. ConnectivityManager connectivity = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
  3. if (connectivity == null) {
  4. Log.i("NetWorkState", "Unavailabel");
  5. return false;
  6. } else {
  7. NetworkInfo[] info = connectivity.getAllNetworkInfo();
  8. if (info != null) {
  9. for (int i = 0; i < info.length; i++) {
  10. if (info[i].getState() == NetworkInfo.State.CONNECTED) {
  11. Log.i("NetWorkState", "Availabel");
  12. return true;
  13. }
  14. }
  15. }
  16. }
  17. return false;
  18. }

public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivity = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
Log.i("NetWorkState", "Unavailabel");
return false;
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
Log.i("NetWorkState", "Availabel");
return true;
}
}
}
}
return false;
}

上面这个方法就是判断网络是否连接的代码,返回true表示有网络,返回false表示无网络。  在Android网络应用程序开发中,经常要判断网络连接是否可用,因此经常有必要监听网络状态的变化。android的网络状态监听可以用BroadcastReceiver来接收网络状态改变的广  播,具体实现如下:

​[html]

view plain

copy

print

?

  1. @Override
  2. public void onReceive(Context context, Intent intent) {
  3. Log.e(TAG, "网络状态改变");
  4. boolean success = false;
  5. //获得网络连接服务
  6. ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
  7. // State state = connManager.getActiveNetworkInfo().getState();
  8. State state = connManager.getNetworkInfo(
  9. ConnectivityManager.TYPE_WIFI).getState(); // 获取网络连接状态
  10. if (State.CONNECTED == state) { // 判断是否正在使用WIFI网络
  11. success = true;
  12. }
  13. state = connManager.getNetworkInfo(
  14. ConnectivityManager.TYPE_MOBILE).getState(); // 获取网络连接状态
  15. if (State.CONNECTED != state) { // 判断是否正在使用GPRS网络
  16. success = true;
  17. }
  18. if (!success) {
  19. Toast.makeText(LocationMapActivity.this, "您的网络连接已中断", Toast.LENGTH_LONG).show();
  20. }
  21. }

@Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG, "网络状态改变");

boolean success = false;

//获得网络连接服务
ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
// State state = connManager.getActiveNetworkInfo().getState();
State state = connManager.getNetworkInfo(
ConnectivityManager.TYPE_WIFI).getState(); // 获取网络连接状态
if (State.CONNECTED == state) { // 判断是否正在使用WIFI网络
success = true;
}

state = connManager.getNetworkInfo(
ConnectivityManager.TYPE_MOBILE).getState(); // 获取网络连接状态
if (State.CONNECTED != state) { // 判断是否正在使用GPRS网络
success = true;
}

if (!success) {
Toast.makeText(LocationMapActivity.this, "您的网络连接已中断", Toast.LENGTH_LONG).show();
}

}

在Activity的onCreate中:

​[html]

view plain

copy

print

?

  1. //注册网络监听
  2. IntentFilter filter = new IntentFilter();
  3. filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
  4. registerReceiver(mNetworkStateReceiver, filter);
  5. //在Activity中的onDestroy中:'
  6. unregisterReceiver(mNetworkStateReceiver); //取消监听

//注册网络监听
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(mNetworkStateReceiver, filter);
//在Activity中的onDestroy中:'

unregisterReceiver(mNetworkStateReceiver); //取消监听

​[html]​ view plain​ ​copy​ ​print​ ​?

  1. public static boolean isNetworkAvailable(Context context) {
  2. ConnectivityManager connectivity = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
  3. if (connectivity == null) {
  4. Log.i("NetWorkState", "Unavailabel");
  5. return false;
  6. } else {
  7. NetworkInfo[] info = connectivity.getAllNetworkInfo();
  8. if (info != null) {
  9. for (int i = 0; i < info.length; i++) {
  10. if (info[i].getState() == NetworkInfo.State.CONNECTED) {
  11. Log.i("NetWorkState", "Availabel");
  12. return true;
  13. }
  14. }
  15. }
  16. }
  17. return false;
  18. }
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivity = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
Log.i("NetWorkState", "Unavailabel");
return false;
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
Log.i("NetWorkState", "Availabel");
return true;
}
}
}
}
return false;
}

上面这个方法就是判断网络是否连接的代码,返回true表示有网络,返回false表示无网络。  在Android网络应用程序开发中,经常要判断网络连接是否可用,因此经常有必要监听网络状态的变化。android的网络状态监听可以用BroadcastReceiver来接收网络状态改变的广  播,具体实现如下:

​[html]​ view plain​ ​copy​ ​print​ ​?

  1. @Override
  2. public void onReceive(Context context, Intent intent) {
  3. Log.e(TAG, "网络状态改变");
  4. boolean success = false;
  5. //获得网络连接服务
  6. ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
  7. // State state = connManager.getActiveNetworkInfo().getState();
  8. State state = connManager.getNetworkInfo(
  9. ConnectivityManager.TYPE_WIFI).getState(); // 获取网络连接状态
  10. if (State.CONNECTED == state) { // 判断是否正在使用WIFI网络
  11. success = true;
  12. }
  13. state = connManager.getNetworkInfo(
  14. ConnectivityManager.TYPE_MOBILE).getState(); // 获取网络连接状态
  15. if (State.CONNECTED != state) { // 判断是否正在使用GPRS网络
  16. success = true;
  17. }
  18. if (!success) {
  19. Toast.makeText(LocationMapActivity.this, "您的网络连接已中断", Toast.LENGTH_LONG).show();
  20. }
  21. }
@Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG, "网络状态改变");

boolean success = false;

//获得网络连接服务
ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
// State state = connManager.getActiveNetworkInfo().getState();
State state = connManager.getNetworkInfo(
ConnectivityManager.TYPE_WIFI).getState(); // 获取网络连接状态
if (State.CONNECTED == state) { // 判断是否正在使用WIFI网络
success = true;
}

state = connManager.getNetworkInfo(
ConnectivityManager.TYPE_MOBILE).getState(); // 获取网络连接状态
if (State.CONNECTED != state) { // 判断是否正在使用GPRS网络
success = true;
}

if (!success) {
Toast.makeText(LocationMapActivity.this, "您的网络连接已中断", Toast.LENGTH_LONG).show();
}

}

在Activity的onCreate中:

​[html]​ view plain​ ​copy​ ​print​ ​?

  1. //注册网络监听
  2. IntentFilter filter = new IntentFilter();
  3. filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
  4. registerReceiver(mNetworkStateReceiver, filter);
  5. //在Activity中的onDestroy中:'
  6. unregisterReceiver(mNetworkStateReceiver); //取消监听
//注册网络监听
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(mNetworkStateReceiver, filter);
//在Activity中的onDestroy中:'

unregisterReceiver(mNetworkStateReceiver); //取消监听