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. | ||
int | ||
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 can vary based on UID, applications should always obtain network information through or . | ||
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 object. | ||
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. |