`
java-mans
  • 浏览: 11434890 次
文章分类
社区版块
存档分类
最新评论

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));
}

}

});

}

}

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

String ACTION_BACKGROUND_DATA_SETTING_CHANGED Broadcast Action: The setting for background data usage has changed values.
String CONNECTIVITY_ACTION A change in network connectivity has occurred.
int DEFAULT_NETWORK_PREFERENCE
String EXTRA_EXTRA_INFO The lookup key for a string that provides optionally supplied extra information about the network state.
String EXTRA_IS_FAILOVER 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.
String EXTRA_NETWORK_INFO This constant is deprecated. Since NetworkInfo can vary based on UID, applications should always obtain network information throughgetActiveNetworkInfo() orgetAllNetworkInfo().
String EXTRA_NO_CONNECTIVITY The lookup key for a boolean that indicates whether there is a complete lack of connectivity, i.e., no network is available.
String EXTRA_OTHER_NETWORK_INFO The lookup key for a NetworkInfo object.
String EXTRA_REASON The lookup key for a string that indicates why an attempt to connect to a network failed.
int TYPE_BLUETOOTH The Default Bluetooth data connection.
int TYPE_DUMMY Dummy data connection.
int TYPE_ETHERNET The Default Ethernet data connection.
int TYPE_MOBILE The Default Mobile data connection.
int TYPE_MOBILE_DUN A DUN-specific Mobile data connection.
int TYPE_MOBILE_HIPRI A High Priority Mobile data connection.
int TYPE_MOBILE_MMS An MMS-specific Mobile data connection.
int TYPE_MOBILE_SUPL A SUPL-specific Mobile data connection.
int TYPE_WIFI The Default WIFI data connection.
int TYPE_WIMAX The Default WiMAX data connection.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics