安卓判断网络状态以及进入配置网络
作者:jao 发布于:2014-8-30 23:38 分类:Andriod开发
文件名:networkInfo.java
package com.jao.other.functions; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.provider.Settings; import android.util.Log; public class networkInfo { public static boolean checkNetworkInfo(Context context){ Context c=context; ConnectivityManager conMan = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE); //mobile 3G Data Network android.net.NetworkInfo.State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState(); Log.v("3G",String.valueOf(mobile)); //CONNECTED ||DISCONNECTED //wifi android.net.NetworkInfo.State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState(); Log.v("WIFI",String.valueOf(wifi)); //NOKNOWN || CONNECTED ||DISCONNECTED //如果3G网络和wifi网络都未连接,且不是处于正在连接状态 则进入Network Setting界面 由用户配置网络连接 if(mobile==android.net.NetworkInfo.State.CONNECTED||mobile==android.net.NetworkInfo.State.CONNECTING) return true; if(wifi==android.net.NetworkInfo.State.CONNECTED||wifi==android.net.NetworkInfo.State.CONNECTING) return true; return false; } public static void changeNetwork(Context context){ context.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));//进入无线网络配置界面 } }
调用方法:
首先是权限配置:<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
boolean state=networkInfo.checkNetworkInfo(context);
if(!state){
networkInfo.changeNetwork(context);
}
我的主页统计
- 建站日期:2012-11-26