android 网络状态获取,Android 获取网络状态信息

package com.uppowerstudio.chapter6.networkconnect;

import android.app.Activity;

import android.content.Context;

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.TextView;

public class MainActivity extends Activity {

// 声明控件变量

private Button queryButton;

private TextView txtAvailable;

private TextView txtNetworkType;

private TextView txtNetworkSubType;

private TextView txtNetworkStatus;

private TextView txtNetworkDetailStatus;

private TextView txtNetworkExtra;

private TextView txtNetworkRoaming;

private ConnectivityManager connect;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

// 加载布局文件main.xml

setContentView(R.layout.main);

// 初始化控件

queryButton = (Button) findViewById(R.id.button_query);

txtAvailable = (TextView) findViewById(R.id.network_available);

txtNetworkType = (TextView) findViewById(R.id.network_type);

txtNetworkSubType = (TextView) findViewById(R.id.network_subtype);

txtNetworkStatus = (TextView) findViewById(R.id.network_status);

txtNetworkDetailStatus = (TextView) findViewById(R.id.network_status_detail);

txtNetworkExtra = (TextView) findViewById(R.id.network_extra);

txtNetworkRoaming = (TextView) findViewById(R.id.network_roaming);

// 实例化ConnectivityManager对象

connect = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

// 设置监听器监听按钮单击事件

queryButton.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// 获取当前可用网络的网络信息数据

NetworkInfo networkInfo = connect.getActiveNetworkInfo();

if (networkInfo != null) {

// 通过isAvilable方法获取网络是否可用的状态

txtAvailable.setText(networkInfo.isAvailable() ? getString(R.string.yes)

: getString(R.string.no));

// 获取当前网络连接的类型

txtNetworkType.setText(networkInfo.getTypeName());

// 获取当前网络连接的子类型(如果存在)

txtNetworkSubType.setText(networkInfo.getSubtypeName());

// 获取网络连接的粗略状态

txtNetworkStatus.setText(networkInfo.getState().toString());

// 获取网络连接的详细状态

txtNetworkDetailStatus.setText(networkInfo

.getDetailedState().toString());

// 获取网络连接的额外附加信息

txtNetworkExtra.setText(networkInfo.getExtraInfo());

// 通过isRoaming方法判断当前是否处于数据漫游状态

txtNetworkRoaming.setText(networkInfo.isRoaming() ? getString(R.string.yes)

: getString(R.string.no));

} else {

// 如果没有可用的网络连接,给予提示

txtAvailable

.setText(getString(R.string.network_unavailable_msg));

}

}

});

}

}

---------------------

Broadcast Action: The setting for background data usage has changed values.

A change in network connectivity has occurred.

The lookup key for a string that provides optionally supplied extra information about the network state.

The lookup key for a boolean that indicates whether a connect event is for a network to which the connectivity manager was failing over following a disconnect on another network.

This constant is deprecated. Since

The lookup key for a boolean that indicates whether there is a complete lack of connectivity, i.e., no network is available.

The lookup key for a string that indicates why an attempt to connect to a network failed.

int

The Default Bluetooth data connection.

int

Dummy data connection.

int

The Default Ethernet data connection.

int

The Default Mobile data connection.

int

A DUN-specific Mobile data connection.

int

A High Priority Mobile data connection.

int

An MMS-specific Mobile data connection.

int

A SUPL-specific Mobile data connection.

int

The Default WIFI data connection.

int

The Default WiMAX data connection.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值