转载 Android开发者论坛上的一篇文章
源链接:Android网络状态判断,讲的细致入微,非常不错,赞一个。
关键代码:
1. android获取当前网络连接状态以及调用网络配置界面
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. Android判断网络状态方法
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. Android 监控网络状态
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; }
android的网络状态监听可以用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(); } }
注册监听:
//注册网络监听 IntentFilter filter = new IntentFilter(); filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); registerReceiver(mNetworkStateReceiver, filter); //在Activity中的onDestroy中:' unregisterReceiver(mNetworkStateReceiver); //取消监听
这里也有一篇:Android杂谈--网络状态判断
上面是转载的部分,下面是原创的部分,也有链接文档。
除了上面的分析外,还有一种情况也可能导致上不了Internet网络:APN 链接点设置错误,今天调试程序时就遇到这种情况。
以联通APN设置为例,如下图,如果接入点选择不是沃3G链接互联网,在不打开WIFI的情况下也无法上网。