1.判断是否打开网络
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings;
public class TestNet {
/**
* 检测网络是否存在
*/
public static void HttpTest(final Activity mActivity)
{
if( !isNetworkAvailable( mActivity) ){
AlertDialog.Builder builders = new AlertDialog.Builder(mActivity);
builders.setTitle("抱歉,网络连接失败,是否进行网络设置?");
builders.setPositiveButton("确定",
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which)
{
//进入无线网络配置界面
mActivity.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
//startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS)); //进入手机中的wifi网络设置界面
}
});
builders.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//关闭当前activity
mActivity.finish();
}
});
builders.show();
}
}
//检测网络是否可用
public static boolean isNetworkAvailable( Activity mActivity ) {
Context context = mActivity.getApplicationContext();
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
return false;
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
}
另外记得在AndroidManifest.xml文件加上
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
这个访问网络权限。
============================
/**
* 获得当前联网类型wifi or mobile
*
* @return -1: 无连接类型; ConnectivityManager.TYPE_WIFI: wifi;
* ConnectivityManager.TYPE_MOBILE:mobile。判断网络连接状态为时mobile最好增加判断是net还是wap网络
*/
public int GetNetWorkType() {
if (getConnectManager() != null) {
NetworkInfo networkInfo = getConnectManager()
.getActiveNetworkInfo();
if (networkInfo != null)
return networkInfo.getType();
return -1;
} else {
return -1;
}
}
2.判断当前网络状态
public class ConnectionChangeReceiver extends
BroadcastReceiver
{ @Override
public void onReceive( Context context, Intent intent ) {
ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService
( Context.CONNECTIVITY_SERVICE ); NetworkInfo activeNetInfo = connectivityManager.
getActiveNetworkInfo();
NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo
( ConnectivityManager.TYPE_MOBILE ); if ( activeNetInfo != null )
{ Toast.makeText( context, "Active Network Type : " +
activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
} if( mobNetInfo != null )
{ Toast.makeText( context, "Mobile Network Type : " +
mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
} }
} < !-- Needed to check when the network connection changes -->
权限添加:
< uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
< receiver android:name="com.blackboard.androidtest.
receiver.ConnectionChangeReceiver" android:label="NetworkConnection">
< intent-filter>
< action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
< /intent-filter>
3.监控网络状态
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivity = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
Log.i("NetWorkState", "Unavailabel");
return false;
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info<i>.getState() == NetworkInfo.State.CONNECTED) {
Log.i("NetWorkState", "Availabel");
return true;
}
}
}
}
return false;
}
4.用BroadcastReceiver来接收网络状态改变的广播
@Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG, "网络状态改变");
boolean success = false;
//获得网络连接服务
ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
// State state = connManager.getActiveNetworkInfo().getState();
State state = connManager.getNetworkInfo(
ConnectivityManager.TYPE_WIFI).getState(); // 获取网络连接状态
if (State.CONNECTED == state) { // 判断是否正在使用WIFI网络
success = true;
}
state = connManager.getNetworkInfo(
ConnectivityManager.TYPE_MOBILE).getState(); // 获取网络连接状态
if (State.CONNECTED != state) { // 判断是否正在使用GPRS网络
success = true;
}
if (!success) {
Toast.makeText(LocationMapActivity.this, "您的网络连接已中断", Toast.LENGTH_LONG).show();
}
}
5.注册监听
//注册网络监听 IntentFilter filter = new IntentFilter(); filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); registerReceiver(mNetworkStateReceiver, filter); //在Activity中的onDestroy中:' unregisterReceiver(mNetworkStateReceiver); //取消监听