4.2.2.6 android中检测网络连接状态简单总结
来源:互联网 发布:淘宝商城地下城装备 编辑:程序博客网 时间:2024/06/05 05:19
android中检测网络连接状态简单总结
分类:
作者同类文章X
- •ViewDragHelper详解
- •详解Android中的屏幕方向
- •android:View的setTag和getTag使用
- •为GridView添加HeaderView
- •用android:clipChildren来实现红心变大特效
- 更多
android中检测网络连接状态简单总结
android检测网络连接wifi3GGPS
相应几乎没有不跟网络打交道的android应用,那么在实际中就需求检测手机是否有网络连接,甚至需要判断是何种方式连接,这样能给用户带来更好的体验和一些使用指导,下面给出一些常用的判断,如果要知道是否有网络、以及是采用wifi连接的还是3G连接的,调用下面对应方法模型就OK了,代码如下:
TestNetworkActivity:
- package com.home.testnetwork;
- import java.util.List;
- import android.app.Activity;
- import android.content.Context;
- import android.location.LocationManager;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- public class TestNetworkActivity extends Activity implements OnClickListener {
- private Button checkBtn;
- private EditText netText;
- private EditText wifiText;
- private EditText net3gText;
- private EditText gpsText;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- 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
- */
- private boolean isNetConnected() {
- ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
- if (cm != null) {
- NetworkInfo[] infos = cm.getAllNetworkInfo();
- if (infos != null) {
- for (NetworkInfo ni : infos) {
- if (ni.isConnected()) {
- return true;
- }
- }
- }
- }
- return false;
- }
- /**
- * 检测wifi是否连接
- *
- * @return
- */
- private boolean isWifiConnected() {
- ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
- if (cm != null) {
- NetworkInfo networkInfo = cm.getActiveNetworkInfo();
- if (networkInfo != null
- && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
- return true;
- }
- }
- return false;
- }
- /**
- * 检测3G是否连接
- *
- * @return
- */
- private boolean is3gConnected() {
- ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
- if (cm != null) {
- NetworkInfo networkInfo = cm.getActiveNetworkInfo();
- if (networkInfo != null
- && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
- return true;
- }
- }
- return false;
- }
- /**
- * 检测GPS是否打开
- *
- * @return
- */
- private boolean isGpsEnabled() {
- LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
- List<String> accessibleProviders = lm.getProviders(true);
- for (String name : accessibleProviders) {
- if ("gps".equals(name)) {
- return true;
- }
- }
- return false;
- }
- @Override
- public void onClick(View v) {
- if (v == checkBtn) {
- netText.setText(isNetConnected() + "");
- wifiText.setText(isWifiConnected() + "");
- net3gText.setText(is3gConnected() + "");
- gpsText.setText(isGpsEnabled() + "");
- }
- }
- }
布局xml:
- <LinearLayout xmlns: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>
记得加上相应权限~
附上图片结果:
0 0
- 4.2.2.6 android中检测网络连接状态简单总结
- android中检测网络连接状态简单总结
- android中检测网络连接状态简单总结
- android中检测网络连接状态简单总结
- Android 检测网络连接状态
- Android 检测网络连接状态
- Android 检测网络连接状态
- Android 检测网络连接状态
- Android 检测网络连接状态
- Android 检测网络连接状态
- android:检测网络连接状态
- android-检测网络连接状态 - 随心
- Android中如何简单检测网络是否连接
- 检测网络连接状态
- 检测网络连接状态
- android检测网络状态
- android 检测网络状态
- android网络状态检测
- php调用R的脚本实现方案
- linux下静态库编译使用复杂版
- LeetCode题解:Path Sum
- nltk(1)——常用函数
- ZigBee中协调器断电重连问题?
- 4.2.2.6 android中检测网络连接状态简单总结
- 【CodeChef】Holes in the text
- Points in Segments (II)(线段树)
- iterator与const_iterator及const iterator区别
- 欢迎使用CSDN-markdown编辑器
- spark源码学习(八):spark具体是如何使用集群的资源去运行任务
- 005_Http之request获取客户端信息07-编码之response编码
- Scalaz(28)- ST Monad :FP方式适用变量
- 面试准备之catalan数相