zoukankan      html  css  js  c++  java
  • android中检测网络连接状态简单总结

    相应几乎没有不跟网络打交道的android应用,那么在实际中就需求检测手机是否有网络连接,甚至需要判断是何种方式连接,这样能给用户带来更好的体验和一些使用指导,下面给出一些常用的判断,如果要知道是否有网络、以及是采用wifi连接的还是3G连接的,调用下面对应方法模型就OK了,代码如下:

    TestNetworkActivity:

    [java] view plaincopy
     
    1. package com.home.testnetwork;  
    2.   
    3. import java.util.List;  
    4.   
    5. import android.app.Activity;  
    6. import android.content.Context;  
    7. import android.location.LocationManager;  
    8. import android.net.ConnectivityManager;  
    9. import android.net.NetworkInfo;  
    10. import android.os.Bundle;  
    11. import android.view.View;  
    12. import android.view.View.OnClickListener;  
    13. import android.widget.Button;  
    14. import android.widget.EditText;  
    15.   
    16. public class TestNetworkActivity extends Activity implements OnClickListener {  
    17.     private Button checkBtn;  
    18.     private EditText netText;  
    19.     private EditText wifiText;  
    20.     private EditText net3gText;  
    21.     private EditText gpsText;  
    22.   
    23.     @Override  
    24.     protected void onCreate(Bundle savedInstanceState) {  
    25.         super.onCreate(savedInstanceState);  
    26.         setContentView(R.layout.main);  
    27.         checkBtn = (Button) findViewById(R.id.main_btn_check);  
    28.         checkBtn.setOnClickListener(this);  
    29.         wifiText = (EditText) findViewById(R.id.main_et_wifi);  
    30.         net3gText = (EditText) findViewById(R.id.main_et_3g);  
    31.         gpsText = (EditText) findViewById(R.id.main_et_GPS);  
    32.         netText = (EditText) findViewById(R.id.main_et_net);  
    33.     }  
    34.   
    35.     /** 
    36.      * 检测网络是否连接 
    37.      *  
    38.      * @return 
    39.      */  
    40.     private boolean isNetConnected() {  
    41.         ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);  
    42.         if (cm != null) {  
    43.             NetworkInfo[] infos = cm.getAllNetworkInfo();  
    44.             if (infos != null) {  
    45.                 for (NetworkInfo ni : infos) {  
    46.                     if (ni.isConnected()) {  
    47.                         return true;  
    48.                     }  
    49.                 }  
    50.             }  
    51.         }  
    52.         return false;  
    53.     }  
    54.   
    55.     /** 
    56.      * 检测wifi是否连接 
    57.      *  
    58.      * @return 
    59.      */  
    60.     private boolean isWifiConnected() {  
    61.         ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);  
    62.         if (cm != null) {  
    63.             NetworkInfo networkInfo = cm.getActiveNetworkInfo();  
    64.             if (networkInfo != null  
    65.                     && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {  
    66.                 return true;  
    67.             }  
    68.         }  
    69.         return false;  
    70.     }  
    71.   
    72.     /** 
    73.      * 检测3G是否连接 
    74.      *  
    75.      * @return 
    76.      */  
    77.     private boolean is3gConnected() {  
    78.         ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);  
    79.         if (cm != null) {  
    80.             NetworkInfo networkInfo = cm.getActiveNetworkInfo();  
    81.             if (networkInfo != null  
    82.                     && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {  
    83.                 return true;  
    84.             }  
    85.         }  
    86.         return false;  
    87.     }  
    88.   
    89.     /** 
    90.      * 检测GPS是否打开 
    91.      *  
    92.      * @return 
    93.      */  
    94.     private boolean isGpsEnabled() {  
    95.         LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
    96.         List<String> accessibleProviders = lm.getProviders(true);  
    97.         for (String name : accessibleProviders) {  
    98.             if ("gps".equals(name)) {  
    99.                 return true;  
    100.             }  
    101.         }  
    102.         return false;  
    103.     }  
    104.   
    105.     @Override  
    106.     public void onClick(View v) {  
    107.         if (v == checkBtn) {  
    108.             netText.setText(isNetConnected() + "");  
    109.             wifiText.setText(isWifiConnected() + "");  
    110.             net3gText.setText(is3gConnected() + "");  
    111.             gpsText.setText(isGpsEnabled() + "");  
    112.         }  
    113.     }  
    114. }  


    布局xml:

    [html] view plaincopy
     
    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    2.     android:layout_width="match_parent"  
    3.     android:layout_height="match_parent"  
    4.     android:orientation="vertical" >  
    5.   
    6.     <Button  
    7.         android:id="@+id/main_btn_check"  
    8.         android:layout_width="match_parent"  
    9.         android:layout_height="wrap_content"  
    10.         android:text="检测网络" />  
    11.   
    12.     <LinearLayout  
    13.         android:layout_width="match_parent"  
    14.         android:layout_height="wrap_content" >  
    15.   
    16.         <TextView  
    17.             android:layout_width="wrap_content"  
    18.             android:layout_height="wrap_content"  
    19.             android:text="网络是否连接:" />  
    20.   
    21.         <EditText  
    22.             android:id="@+id/main_et_net"  
    23.             android:layout_width="match_parent"  
    24.             android:layout_height="wrap_content"  
    25.             android:layout_marginLeft="10dp"  
    26.             android:enabled="false" />  
    27.     </LinearLayout>  
    28.   
    29.     <LinearLayout  
    30.         android:layout_width="match_parent"  
    31.         android:layout_height="wrap_content" >  
    32.   
    33.         <TextView  
    34.             android:layout_width="wrap_content"  
    35.             android:layout_height="wrap_content"  
    36.             android:text="wifi是否连接:" />  
    37.   
    38.         <EditText  
    39.             android:id="@+id/main_et_wifi"  
    40.             android:layout_width="match_parent"  
    41.             android:layout_height="wrap_content"  
    42.             android:layout_marginLeft="10dp"  
    43.             android:enabled="false" />  
    44.     </LinearLayout>  
    45.   
    46.     <LinearLayout  
    47.         android:layout_width="match_parent"  
    48.         android:layout_height="wrap_content" >  
    49.   
    50.         <TextView  
    51.             android:layout_width="wrap_content"  
    52.             android:layout_height="wrap_content"  
    53.             android:text="3G是否连接:" />  
    54.   
    55.         <EditText  
    56.             android:id="@+id/main_et_3g"  
    57.             android:layout_width="match_parent"  
    58.             android:layout_height="wrap_content"  
    59.             android:layout_marginLeft="10dp"  
    60.             android:enabled="false" />  
    61.     </LinearLayout>  
    62.   
    63.     <LinearLayout  
    64.         android:layout_width="match_parent"  
    65.         android:layout_height="wrap_content" >  
    66.   
    67.         <TextView  
    68.             android:layout_width="wrap_content"  
    69.             android:layout_height="wrap_content"  
    70.             android:text="GPS是否打开:" />  
    71.   
    72.         <EditText  
    73.             android:id="@+id/main_et_GPS"  
    74.             android:layout_width="match_parent"  
    75.             android:layout_height="wrap_content"  
    76.             android:layout_marginLeft="10dp"  
    77.             android:enabled="false" />  
    78.     </LinearLayout>  
    79.   
    80. </LinearLayout>  


    记得加上相应权限~

    附上图片结果:

     

  • 相关阅读:
    php 对象转数组
    一张图解析FastAdmin中的表格列表的功能
    tp中打印sql,查看语句信息
    fastadmin 增加外键表搜索
    fastadmin 后台管理中,权限设置,不同管理员,显示不同的数据
    Python exec 函数
    Python eval() 函数
    Python next() 函数
    Python iter() 函数
    Python range() 函数
  • 原文地址:https://www.cnblogs.com/zhuzhengwen1983/p/3617953.html
Copyright © 2011-2022 走看看