1.myReceiver.java 广播
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (isNetworkConnected(context)) {
int type = getConnectedType(context);
switch (type) {
case -1:
Toast.makeText(context, "无有网络连接!请手动连接网络", Toast.LENGTH_SHORT)
.show();
break;
case 0:
Toast.makeText(context, "已经接入手机网络,请注意流量!", Toast.LENGTH_SHORT)
.show();
break;
case 1:
Toast.makeText(context, "WIFI网络", Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(context, "wap网络", Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(context, "net网络", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
} else {
Toast.makeText(context, "无网络连接!请手动连接网络", Toast.LENGTH_SHORT).show();
}
}
public boolean isNetworkConnected(Context context) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.isAvailable();
}
return false;
}
public static int getConnectedType(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager
.getActiveNetworkInfo();
if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
return mNetworkInfo.getType();
}
}
return -1;
}
}
2.在入口activity.java注册广播
public class MainActivity extends Activity {
TextView textView;
Button button;
IntentFilter intentFilter;
MyReceiver myReceiver = new MyReceiver();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
public void initView() {
textView = (TextView) findViewById(R.id.tv);
button = (Button) findViewById(R.id.btn);
intentFilter = new IntentFilter();
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
//为BroadcastReceiver指定action,使之用于接收同action的广播
//registerReceiver(myReceiver, intentFilter);
Intent service = new Intent(this, MyReceiver.class);
startService(service);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (isNetworkConnected(getApplicationContext())) {
// -1:没有网络 0:手机网络 1:WIFI网络 2:wap网络 3:net网络
int type = getConnectedType(getApplicationContext());
switch (type) {
case -1:
textView.setText("无网络接入" + type);
Toast.makeText(getApplicationContext(),
"无有网络连接!请手动连接网络", Toast.LENGTH_SHORT).show();
break;
case 0:
textView.setText("已经接入手机网络,请注意流量!" + type);
Toast.makeText(getApplicationContext(),
"已经接入手机网络,请注意流量!", Toast.LENGTH_SHORT).show();
break;
case 1:
textView.setText("WIFI网络" + type);
Toast.makeText(getApplicationContext(), "WIFI网络",
Toast.LENGTH_SHORT).show();
break;
case 2:
textView.setText("wap网络" + type);
Toast.makeText(getApplicationContext(), "wap网络",
Toast.LENGTH_SHORT).show();
break;
case 3:
textView.setText("net网络" + type);
Toast.makeText(getApplicationContext(), "net网络",
Toast.LENGTH_SHORT).show();
break;
default:
break;
}
} else {
textView.setText("无网络接入");
Toast.makeText(getApplicationContext(), "无有网络连接!请手动连接网络",
Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
protected void onStop() {
super.onStart();
/**
* 卸载广播
*/
unregisterReceiver(myReceiver);
}
/**
* 判断有无网络连接
*
* @param context
* @return
*/
public boolean isNetworkConnected(Context context) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.isAvailable();
}
return false;
}
//
// public boolean isMobileConnected(Context context) {
// if (context != null) {
// ConnectivityManager mConnectivityManager = (ConnectivityManager) context
// .getSystemService(Context.CONNECTIVITY_SERVICE);
// NetworkInfo mMoBileNetworkInfo = mConnectivityManager
// .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
// if (mMoBileNetworkInfo != null) {
// return mMoBileNetworkInfo.isAvailable();
// }
// }
// return false;
// }
//
// public boolean isWifiConnected(Context context) {
// if (context != null) {
// ConnectivityManager mConnectivityManager = (ConnectivityManager) context
// .getSystemService(Context.CONNECTIVITY_SERVICE);
// NetworkInfo mWiFiNetworkInfo = mConnectivityManager
// .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
// if (mWiFiNetworkInfo != null) {
// return mWiFiNetworkInfo.isAvailable();
// }
// }
// return false;
// }
public static int getConnectedType(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager
.getActiveNetworkInfo();
if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
return mNetworkInfo.getType();
}
}
return -1;
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.zdemo.network"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.zdemo.network.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 注册网络判断的广播 start -->
<receiver android:name="util.MyReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<!-- 注册网络判断的广播 end -->
</application>
</manifest>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击监测网络状态"/>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:layout_below="@id/btn"/>
</RelativeLayout>
把代码拷贝运行就OK了。