通过Broadcast动态注册监听网络变化和网络类型

package com.beini.networkchanges;

创新互联-专业网站定制、快速模板网站建设、高性价比龙沙网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式龙沙网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖龙沙地区。费用合理售后完善,十载实体公司更值得信赖。

import android.app.Activity;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.content.IntentFilter;

import android.net.ConnectivityManager;

import android.net.NetworkInfo;

import android.os.Bundle;

import android.telephony.TelephonyManager;

import android.util.Log;

import android.widget.TextView;

/**

 * 

 * @author beini

 * 

 */

public class MainActivity extends Activity {

private NetworkChangeReceive networkChangeReceive;

private TextView txtShowNetWorkeSate;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

txtShowNetWorkeSate = (TextView) findViewById(R.id.txt_showNetWorkeSate);

inintReceiver();

}

private void inintReceiver() {

IntentFilter intentFilter = new IntentFilter();

intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");

networkChangeReceive = new NetworkChangeReceive();

registerReceiver(networkChangeReceive, intentFilter);

}

class NetworkChangeReceive extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

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

NetworkInfo networkInfo = connectivityManager

.getActiveNetworkInfo();

if (networkInfo != null && networkInfo.isAvailable()) {

txtShowNetWorkeSate.setText(getString(R.string.net_aviable)

+ getNetWorkType(networkInfo));

} else {

txtShowNetWorkeSate.setText(getString(R.string.net_unaviable));

}

}

}

@Override

protected void onDestroy() {

super.onDestroy();

unregisterReceiver(networkChangeReceive);

}

/**

* 获取网络 2g,3g, 4g;通过networkInfo .getSubtype()会得到网络的类型,

* 用networkInfo.getSubtypeName()会得到网络的名字。

*/

public String getNetWorkType(NetworkInfo networkInfo) {

// int subType = networkInfo.getSubtype();

// String subTypeName = networkInfo.getSubtypeName();

//

int type = networkInfo.getType();

// String typeName = networkInfo.getTypeName();

//

// Log.i("beini", "subType=" + type + "   " + "typeName=" + typeName);

// Log.i("beini", "subType=" + subType + "   " + "subTypeName="

// + subTypeName);

if (type == ConnectivityManager.TYPE_WIFI) {

return getString(R.string.net_wifi);

} else if (type == ConnectivityManager.TYPE_MOBILE) {

return getnetworkChange();

}

return "";

}

public String getnetworkChange() {

TelephonyManager mTelephonyManager = (TelephonyManager) getApplication()

.getSystemService(Context.TELEPHONY_SERVICE);

int networkType = mTelephonyManager.getNetworkType();

switch (networkType) {

case TelephonyManager.NETWORK_TYPE_GPRS:

case TelephonyManager.NETWORK_TYPE_EDGE:

case TelephonyManager.NETWORK_TYPE_CDMA:

case TelephonyManager.NETWORK_TYPE_1xRTT:

case TelephonyManager.NETWORK_TYPE_IDEN:

return "2G";

case TelephonyManager.NETWORK_TYPE_UMTS:

case TelephonyManager.NETWORK_TYPE_EVDO_0:

case TelephonyManager.NETWORK_TYPE_EVDO_A:

case TelephonyManager.NETWORK_TYPE_HSDPA:

case TelephonyManager.NETWORK_TYPE_HSUPA:

case TelephonyManager.NETWORK_TYPE_HSPA:

case TelephonyManager.NETWORK_TYPE_EVDO_B:

case TelephonyManager.NETWORK_TYPE_EHRPD:

case TelephonyManager.NETWORK_TYPE_HSPAP:

return "3G";

case TelephonyManager.NETWORK_TYPE_LTE:

return "4G";

default:

return "Unknown";

}

}

}


当前名称:通过Broadcast动态注册监听网络变化和网络类型
标题链接:http://hbruida.cn/article/ghhocp.html