判断Android设备是否连接网络


[java]
  view plain copy
  1. /** 
  2.   * 判断Android客户端网络是否连接  
  3.   * @param context 
  4.   * @return 真假 
  5.   */public static boolean checkNet(Context context) {     
  6.              
  7.         try {     
  8.             ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);     
  9.             if (connectivity != null) {     
  10.                  
  11.                 NetworkInfo info = connectivity.getActiveNetworkInfo();     
  12.                 if (info != null && info.isConnected()) {     
  13.                      
  14.                     if (info.getState() == NetworkInfo.State.CONNECTED) {     
  15.                         return true;     
  16.                     }     
  17.                 }     
  18.             }     
  19.         } catch (Exception e) {     
  20.         return false;     
  21. }     
  22.         return false;     
  23.     }    

以上代码只能判断是否有可用的连接,而不能判断是否能连网

 

[java]  view plain copy
  1. * 检验网络连接 并toast提示  
  2. *   
  3. @return  
  4. */  
  5. public boolean note_Intent(Context context) {  
  6.     ConnectivityManager con = (ConnectivityManager) context  
  7.         .getSystemService(Context.CONNECTIVITY_SERVICE);  
  8.     NetworkInfo networkinfo = con.getActiveNetworkInfo();  
  9.     if (networkinfo == null || !networkinfo.isAvailable()) {  
  10.     // 当前网络不可用  
  11.         Toast.makeText(context.getApplicationContext(), "请先连接Internet!",  
  12.         Toast.LENGTH_SHORT).show();  
  13.         return false;  
  14.     }  
  15.     boolean wifi = con.getNetworkInfo(ConnectivityManager.TYPE_WIFI)  
  16.         .isConnectedOrConnecting();  
  17.     if (!wifi) { // 提示使用wifi  
  18.         Toast.makeText(context.getApplicationContext(), "建议您使用WIFI以减少流量!",  
  19.         Toast.LENGTH_SHORT).show();  
  20.     }  
  21.     return true;  
  22.   
  23. }  

以上转自:http://www.iteye.com/topic/1117733

 

android中判断网络连接是否可用

http://www.cnblogs.com/codeworker/archive/2012/04/23/2467180.html
一、判断网络连接是否可用

[java]  view plain copy
  1. public static boolean isNetworkAvailable(Context context) {     
  2.         ConnectivityManager cm = (ConnectivityManager) context     
  3.                 .getSystemService(Context.CONNECTIVITY_SERVICE);     
  4.         if (cm == null) {     
  5.         } else {  
  6.        //如果仅仅是用来判断网络连接  
  7.         //则可以使用 cm.getActiveNetworkInfo().isAvailable();    
  8.             NetworkInfo[] info = cm.getAllNetworkInfo();     
  9.             if (info != null) {     
  10.                 for (int i = 0; i < info.length; i++) {     
  11.                     if (info[i].getState() == NetworkInfo.State.CONNECTED) {     
  12.                         return true;     
  13.                     }     
  14.                 }     
  15.             }     
  16.         }     
  17.         return false;     
  18.     }   

二、判断GPS是否打开

[java]  view plain copy
  1. public static boolean isGpsEnabled(Context context) {     
  2.         LocationManager lm = ((LocationManager) context     
  3.                 .getSystemService(Context.LOCATION_SERVICE));     
  4.         List<String> accessibleProviders = lm.getProviders(true);     
  5.         return accessibleProviders != null && accessibleProviders.size() > 0;     
  6.     }   

三、判断WIFI是否打开

[java]  view plain copy
  1. public static boolean isWifiEnabled(Context context) {     
  2.         ConnectivityManager mgrConn = (ConnectivityManager) context     
  3.                 .getSystemService(Context.CONNECTIVITY_SERVICE);     
  4.         TelephonyManager mgrTel = (TelephonyManager) context     
  5.                 .getSystemService(Context.TELEPHONY_SERVICE);     
  6.         return ((mgrConn.getActiveNetworkInfo() != null && mgrConn     
  7.                 .getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel     
  8.                 .getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);     
  9.     }  

 四、判断是否是3G网络

[java]  view plain copy
  1. public static boolean is3rd(Context context) {     
  2.         ConnectivityManager cm = (ConnectivityManager) context     
  3.                 .getSystemService(Context.CONNECTIVITY_SERVICE);     
  4.         NetworkInfo networkINfo = cm.getActiveNetworkInfo();     
  5.         if (networkINfo != null     
  6.                 && networkINfo.getType() == ConnectivityManager.TYPE_MOBILE) {     
  7.             return true;     
  8.         }     
  9.         return false;     
  10.     }    


五、判断是wifi还是3g网络,用户的体现性在这里了,wifi就可以建议下载或者在线播放。

[java]  view plain copy
  1. public static boolean isWifi(Context context) {     
  2.             ConnectivityManager cm = (ConnectivityManager) context     
  3.                     .getSystemService(Context.CONNECTIVITY_SERVICE);     
  4.             NetworkInfo networkINfo = cm.getActiveNetworkInfo();     
  5.             if (networkINfo != null     
  6.                     && networkINfo.getType() == ConnectivityManager.TYPE_WIFI) {     
  7.                 return true;     
  8.             }     
  9.             return false;     
  10.         }  

【转自】http://blog.csdn.net/skiffloveblue/article/details/7904492


Android平台上开发基于网络的应用,必然需要去判断当前的网络连接情况。

[java]  view plain copy
  1. package com.example.network;  
  2.   
  3. import android.content.Context;  
  4. import android.net.ConnectivityManager;  
  5. import android.net.NetworkInfo;  
  6.   
  7. public class NetworkUtil {  
  8.   
  9.     /** 
  10.      * 检查网络是否可用 
  11.      *  
  12.      * @param context 
  13.      * @return 
  14.      */  
  15.     public static boolean isNetworkAvailable(Context context) {  
  16.   
  17.         ConnectivityManager manager = (ConnectivityManager) context  
  18.                 .getApplicationContext().getSystemService(  
  19.                         Context.CONNECTIVITY_SERVICE);  
  20.   
  21.         if (manager == null) {  
  22.             return false;  
  23.         }  
  24.   
  25.         NetworkInfo networkinfo = manager.getActiveNetworkInfo();  
  26.   
  27.         if (networkinfo == null || !networkinfo.isAvailable()) {  
  28.             return false;  
  29.         }  
  30.   
  31.         return true;  
  32.     }  
  33.       
  34. }  


当网络不可用的时候,我们应该提供一个 界面让用户区设置网络连接。Android 2.3以上跟Android 2.3以前的设置方式是不一样的,我们需要判断一下。

[java]  view plain copy
  1. package com.example.network;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.app.AlertDialog;  
  6. import android.app.AlertDialog.Builder;  
  7. import android.content.ComponentName;  
  8. import android.content.Context;  
  9. import android.content.DialogInterface;  
  10. import android.content.Intent;  
  11. import android.util.Log;  
  12. import android.view.Menu;  
  13. import android.widget.Toast;  
  14.   
  15. public class MainActivity extends Activity {  
  16.   
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_main);  
  21.     }  
  22.   
  23.     @Override  
  24.     protected void onStart() {  
  25.   
  26.         Log.i("MainActivity""onStart");  
  27.   
  28.         if (!NetworkUtil.isNetworkAvailable(this)) {  
  29.             showSetNetworkUI(this);  
  30.         } else {  
  31.             Toast.makeText(this"网络可用..."0).show();  
  32.         }  
  33.   
  34.         super.onStart();  
  35.     }  
  36.   
  37.     @Override  
  38.     protected void onResume() {  
  39.   
  40.         Log.i("MainActivity""onStart");  
  41.   
  42.         super.onResume();  
  43.     }  
  44.   
  45.     /* 
  46.      * 打开设置网络界面 
  47.      */  
  48.     public void showSetNetworkUI(final Context context) {  
  49.         // 提示对话框  
  50.         AlertDialog.Builder builder = new Builder(context);  
  51.         builder.setTitle("网络设置提示")  
  52.                 .setMessage("网络连接不可用,是否进行设置?")  
  53.                 .setPositiveButton("设置"new DialogInterface.OnClickListener() {  
  54.   
  55.                     @Override  
  56.                     public void onClick(DialogInterface dialog, int which) {  
  57.                         // TODO Auto-generated method stub  
  58.                         Intent intent = null;  
  59.                         // 判断手机系统的版本 即API大于10 就是3.0或以上版本  
  60.                         if (android.os.Build.VERSION.SDK_INT > 10) {  
  61.                             intent = new Intent(  
  62.                                     android.provider.Settings.ACTION_WIFI_SETTINGS);  
  63.                         } else {  
  64.                             intent = new Intent();  
  65.                             ComponentName component = new ComponentName(  
  66.                                     "com.android.settings",  
  67.                                     "com.android.settings.WirelessSettings");  
  68.                             intent.setComponent(component);  
  69.                             intent.setAction("android.intent.action.VIEW");  
  70.                         }  
  71.                         context.startActivity(intent);  
  72.                     }  
  73.                 })  
  74.                 .setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  75.   
  76.                     @Override  
  77.                     public void onClick(DialogInterface dialog, int which) {  
  78.   
  79.                         dialog.dismiss();  
  80.                     }  
  81.                 }).show();  
  82.     }  
  83.   
  84.     @Override  
  85.     public boolean onCreateOptionsMenu(Menu menu) {  
  86.         // Inflate the menu; this adds items to the action bar if it is present.  
  87.         getMenuInflater().inflate(R.menu.main, menu);  
  88.         return true;  
  89.     }  
  90.   
  91. }  


记得在 Manifest文件中加入以下权限

[html]  view plain copy
  1. <uses-permission android:name="android.permission.INTERNET"/>  
  2.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>  

http://blog.csdn.net/top_code/article/details/9015381

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值