判断当前网络是否可用

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

有两种方式

方式一,是否有网络(所有网络mobilewifi

public boolean  isNetWorkConnected() {
ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
return manager!=null&&(manager.getActiveNetworkInfo().isConnected());
}

 

 

第二种方式,是否有指定的网络

检查wifi是否可用的两种方式

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
boolean wifiEnabled = wifiManager.isWifiEnabled();
if(wifiEnabled)
{
//Wifi可用
}else {
//Wifi不可用
}

 

/**

 * 判断WIFI是否连接

 * 

 * @param context

 * @return

 */

private static boolean isWIFIConnected(Context context) {

ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo info = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (info != null) {

return info.isConnected();

}

return false;

}

 

-------------------------------------------

package com.ithm.lottery17.util;

 

import com.ithm.lottery17.GloableParams;

 

import android.content.ContentResolver;

import android.content.Context;

import android.database.Cursor;

import android.net.ConnectivityManager;

import android.net.NetworkInfo;

import android.net.Uri;

/**

 *  网络判断工具

 * @author Administrator

 *

 */

public class NetUtil {

 

private static final Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");

 

/**

 * 判断网络 是否含有网络 是那种通信渠道

 * 

 * @return

 */

public static boolean cheackNet(Context context) {

// 判断wifi是否连接

boolean isWIFI = isWIFIConnected(context);

// 判断mobile apn是否可以连接

boolean isAPN = isMobileConnected(context);

// 如果均为false,提示用户设置网络

if (isAPN == false && isWIFI == false) {

// 提示用户设置网络

return false;

}

// 如果mobile apntruewap方式

if (isAPN) {

// wap方式,代理信息,ip和端口

// 获取到当前正在处于连接的那个方式

// 获取到正在处于连接的方式的代理信息(10.0.0.172 010.000.000.172

setProxyInfo(context);

}

return true;

}

 

private static void setProxyInfo(Context context) {

ContentResolver contentResolver = context.getContentResolver();

// 获取到当前正在处于连接的那个方式

Cursor query = contentResolver.query(PREFERRED_APN_URI, null, null, null, null);

if (query != null && query.moveToNext()) {

GloableParams.PROXY_IP = query.getString(query.getColumnIndex("proxy"));

GloableParams.PROXY_PORT = query.getInt(query.getColumnIndex("port"));

}

}

 

/**

 * 判断WIFI是否连接

 * 

 * @param context

 * @return

 */

private static boolean isWIFIConnected(Context context) {

ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo info = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (info != null) {

return info.isConnected();

}

return false;

}

 

/**

 * 判断MOBILE是否连接

 * 

 * @param context

 * @return

 */

private static boolean isMobileConnected(Context context) {

ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo info = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

if (info != null) {

return info.isConnected();

}

return false;

}

//WIFIMOBILE并存——手机厂商支持

}

------------------检查网络并提示用户设置网络------------

package com.example.networktest;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.database.Cursor;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

/**
* 测试网络
* @param view
*/
public void click1(View view) {
ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
if(info!=null&&info.isConnected()){
Toast.makeText(getApplicationContext(), "网络已连接", 1).show();
}else {
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("设置网络");
builder.setMessage("网络错误,请检查网络设置");
builder.setPositiveButton("设置网络", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// <action android:name="android.intent.action.MAIN" />
// <action android:name="android.settings.WIRELESS_SETTINGS" />
// <action android:name="android.settings.AIRPLANE_MODE_SETTINGS" />
// <category android:name="android.intent.category.DEFAULT" />
// <category android:name="android.intent.category.VOICE_LAUNCH" />
Intent intent = new Intent();
intent.setAction("android.intent.action.MAIN");
intent.setAction("android.settings.WIRELESS_SETTINGS");
intent.setAction("android.settings.AIRPLANE_MODE_SETTINGS");
intent.addCategory("android.intent.category.DEFAULT");
intent.addCategory("android.intent.category.VOICE_LAUNCH");
startActivity(intent);

}
});
builder.setNegativeButton("取消", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {

}
});
builder.create();
builder.show();
}
}

/**
* 测试wifi
* @param view
*/
public void click2(View view) {
ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(info!=null&&info.isConnected()) {
Toast.makeText(getApplicationContext(), "wifi网络可用", 1).show();
}else {
AlertDialog.Builder builder = new Builder(MainActivity.this);
builder.setTitle("wifi设置");
builder.setMessage("wifi没有开启请设置wifi");
builder.setNegativeButton("取消", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {

}
});
builder.setPositiveButton("设置wfifi", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent();
// <intent-filter>
// <action android:name="android.intent.action.MAIN" />
// <action android:name="android.settings.WIFI_SETTINGS" />
// <action android:name="android.net.wifi.PICK_WIFI_NETWORK" />
// <category android:name="android.intent.category.DEFAULT" />
// <category android:name="android.intent.category.VOICE_LAUNCH" />
// <category android:name="com.android.settings.SHORTCUT" />
// </intent-filter>
intent.setAction("android.intent.action.MAIN");
intent.setAction("android.settings.WIFI_SETTINGS");
intent.setAction("android.net.wifi.PICK_WIFI_NETWORK");
intent.addCategory("android.intent.category.DEFAULT");
intent.addCategory("android.intent.category.VOICE_LAUNCH");
intent.addCategory("com.android.settings.SHORTCUT");
startActivity(intent);
}
});
builder.show();
}
}

/**
* 测试Mobile
* @param view
*/
public void click3(View view) {
ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(info!=null&&info.isConnected()) {
Uri uri = Uri.parse("content://telephony/carriers/preferapn");
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if(cursor!=null&&cursor.moveToNext()) {
Toast.makeText(getApplicationContext(), "当前是net方式", 1).show();
}else {
Toast.makeText(getApplicationContext(), "当前是wap方式", 1).show();
}
}
}

}