判断当前网络是否可用

时间:2021-02-04 22:17:17

今天遇到了需要判断当前网络是否可用的情况,如果网络可用,则点击进行跳转,如果网络不可用,则点击不进行跳转。

自己的实现过程:

    public boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager == null) {
return false;
} else {
// 获取NetworkInfo对象
NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();
if (networkInfo != null && networkInfo.length > 0) {
for (int i = 0; i < networkInfo.length; i++) {
if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
通过判断isNetworkAvailable为true或false进而判断是否有网络存在。

    @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.portrait:
if (isNetworkAvailable(IndexPageActivity.this)) {
Intent intent = new Intent();
intent.putExtra("url", AccountCons.PORTRAIT_URI);
intent.setClass(IndexPageActivity.this, WebViewActivity.class);
startActivity(intent);

} else {
Toast.makeText(this, "当前无网络,无法跳转", Toast.LENGTH_SHORT).show();
}
break;

case R.id.bind_account:
if (isNetworkAvailable(IndexPageActivity.this)) {
Toast.makeText(this, "跳转至三方绑定界面", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "当前无网络,无法跳转", Toast.LENGTH_SHORT).show();
}
break;
case R.id.exit_account_bt:
showExitAccoutDialog();
break;
}
}


学习:


转载自:http://www.cnblogs.com/renqingping/archive/2012/10/18/Net.html 


1. 获取ConnectivityManager对象

Context context = activity.getApplicationContext();
// 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)
ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

2. 获取NetworkInfo对象

// 获取NetworkInfo对象
NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();


3. 判断当前网络是否连接

if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}


4. 在AndroidManifest.xml中添加访问网络状态权限

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

代码示例:

public class ClassTestDemoActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (isNetworkAvailable(ClassTestDemoActivity.this))
{
Toast.makeText(getApplicationContext(), "当前有可用网络!", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "当前没有可用网络!", Toast.LENGTH_LONG).show();
}
}

/**
* 检查当前网络是否可用
*
* @param context
* @return
*/

public boolean isNetworkAvailable(Activity activity)
{
Context context = activity.getApplicationContext();
// 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

if (connectivityManager == null)
{
return false;
}
else
{
// 获取NetworkInfo对象
NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();

if (networkInfo != null && networkInfo.length > 0)
{
for (int i = 0; i < networkInfo.length; i++)
{
System.out.println(i + "===状态===" + networkInfo[i].getState());
System.out.println(i + "===类型===" + networkInfo[i].getTypeName());
// 判断当前网络状态是否为连接状态
if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
}
}
return false;
}
}
运行结果:

0===状态===UNKNOWN

0===类型===mobile

1===状态===CONNECTED

1===类型===WIFI


转载自:http://www.cnblogs.com/codeworker/archive/2012/04/23/2467180.html


1. 判断网络连接是否可用

    public static boolean isNetworkAvailable(Context context) {   
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null) {
} else {
       //如果仅仅是用来判断网络连接
       //则可以使用 cm.getActiveNetworkInfo().isAvailable();
NetworkInfo[] info = cm.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
2. 判断GPS是否打开

    public static boolean isGpsEnabled(Context context) {   
LocationManager lm = ((LocationManager) context
.getSystemService(Context.LOCATION_SERVICE));
List<String> accessibleProviders = lm.getProviders(true);
return accessibleProviders != null && accessibleProviders.size() > 0;
}
3. 判断WIFI是否打开

    public static boolean isWifiEnabled(Context context) {   
ConnectivityManager mgrConn = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
TelephonyManager mgrTel = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
return ((mgrConn.getActiveNetworkInfo() != null && mgrConn
.getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel
.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);
}
4. 判断3G网络是否可用

    public static boolean is3rd(Context context) {   
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkINfo = cm.getActiveNetworkInfo();
if (networkINfo != null
&& networkINfo.getType() == ConnectivityManager.TYPE_MOBILE) {
return true;
}
return false;
}
5. 判断是WIFI还是3G网络

        public static boolean isWifi(Context context) {   
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkINfo = cm.getActiveNetworkInfo();
if (networkINfo != null
&& networkINfo.getType() == ConnectivityManager.TYPE_WIFI) {
return true;
}
return false;
}