android中检测网络连接状态简单总结
16lz
2021-01-23
android中检测网络连接状态简单总结
分类:Android 2013-10-13 13:12 143人阅读 评论(0) 收藏 举报 android 检测网络连接 wifi 3G GPS相应几乎没有不跟网络打交道的android应用,那么在实际中就需求检测手机是否有网络连接,甚至需要判断是何种方式连接,这样能给用户带来更好的体验和一些使用指导,下面给出一些常用的判断,如果要知道是否有网络、以及是采用wifi连接的还是3G连接的,调用下面对应方法模型就OK了,代码如下:
TestNetworkActivity:
[java] view plain copy- packagecom.home.testnetwork;
- importjava.util.List;
- importandroid.app.Activity;
- importandroid.content.Context;
- importandroid.location.LocationManager;
- importandroid.net.ConnectivityManager;
- importandroid.net.NetworkInfo;
- importandroid.os.Bundle;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.widget.Button;
- importandroid.widget.EditText;
- publicclassTestNetworkActivityextendsActivityimplementsOnClickListener{
- privateButtoncheckBtn;
- privateEditTextnetText;
- privateEditTextwifiText;
- privateEditTextnet3gText;
- privateEditTextgpsText;
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- checkBtn=(Button)findViewById(R.id.main_btn_check);
- checkBtn.setOnClickListener(this);
- wifiText=(EditText)findViewById(R.id.main_et_wifi);
- net3gText=(EditText)findViewById(R.id.main_et_3g);
- gpsText=(EditText)findViewById(R.id.main_et_GPS);
- netText=(EditText)findViewById(R.id.main_et_net);
- }
- /**
- *检测网络是否连接
- *
- *@return
- */
- privatebooleanisNetConnected(){
- ConnectivityManagercm=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
- if(cm!=null){
- NetworkInfo[]infos=cm.getAllNetworkInfo();
- if(infos!=null){
- for(NetworkInfoni:infos){
- if(ni.isConnected()){
- returntrue;
- }
- }
- }
- }
- returnfalse;
- }
- /**
- *检测wifi是否连接
- *
- *@return
- */
- privatebooleanisWifiConnected(){
- ConnectivityManagercm=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
- if(cm!=null){
- NetworkInfonetworkInfo=cm.getActiveNetworkInfo();
- if(networkInfo!=null
- &&networkInfo.getType()==ConnectivityManager.TYPE_WIFI){
- returntrue;
- }
- }
- returnfalse;
- }
- /**
- *检测3G是否连接
- *
- *@return
- */
- privatebooleanis3gConnected(){
- ConnectivityManagercm=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
- if(cm!=null){
- NetworkInfonetworkInfo=cm.getActiveNetworkInfo();
- if(networkInfo!=null
- &&networkInfo.getType()==ConnectivityManager.TYPE_MOBILE){
- returntrue;
- }
- }
- returnfalse;
- }
- /**
- *检测GPS是否打开
- *
- *@return
- */
- privatebooleanisGpsEnabled(){
- LocationManagerlm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
- List<String>accessibleProviders=lm.getProviders(true);
- for(Stringname:accessibleProviders){
- if("gps".equals(name)){
- returntrue;
- }
- }
- returnfalse;
- }
- @Override
- publicvoidonClick(Viewv){
- if(v==checkBtn){
- netText.setText(isNetConnected()+"");
- wifiText.setText(isWifiConnected()+"");
- net3gText.setText(is3gConnected()+"");
- gpsText.setText(isGpsEnabled()+"");
- }
- }
- }
布局xml:
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <Button
- android:id="@+id/main_btn_check"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="检测网络"/>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="网络是否连接:"/>
- <EditText
- android:id="@+id/main_et_net"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dp"
- android:enabled="false"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="wifi是否连接:"/>
- <EditText
- android:id="@+id/main_et_wifi"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dp"
- android:enabled="false"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="3G是否连接:"/>
- <EditText
- android:id="@+id/main_et_3g"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dp"
- android:enabled="false"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="GPS是否打开:"/>
- <EditText
- android:id="@+id/main_et_GPS"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dp"
- android:enabled="false"/>
- </LinearLayout>
- </LinearLayout>
记得加上相应权限~
附上图片结果:
分享到:
更多相关文章
- Android之UI学习篇六:ImageView实现图片旋转和缩放
- 网络界面Android 网络连接--Wifi/3G
- 快速开发框架Afinal的使用(数据库操作,HTTP请求,网络图片加载,控件绑
- Android中的网络时间同步 !!!!!!!!
- Android网络编程之Http通信
- Android bitmap图片处理
- Android网络相关---上网流程
- Android 中,应用程序需要的图片资源如何针对不同屏幕大小手机设计