❶ android 判断当前的网络连接是wifi还是3g,4g csdn
/**
* 判断是否是wifi连接
*/
public static boolean isWifi(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null)
return false;
return connectivity.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI;
}
/**
* 判断网络是否连接
*
*/
public static boolean isConnected(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (null != connectivity) {
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (null != info && info.isConnected()) {
if (info.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
return false;
}
❷ android 如何判断当前网络比如说是WI-FI还是移动
先取的所有连接 NetworkInfo[] networkInfos = (); 对每一个networkInfo 判断 networkInfogetState() == NetworkInfoStateCONNECTED 判断哪个是正在连接着的 然后用 networkInfogetType() 获得 是 Conandroid 如何判断当前网络?比如说是WI-FI还是移动
❸ android 怎么判断网络状态
获取android系统的连接服务可判断网络连接状态,代码如下
public class NetUtils{
public static boolean isNetworkConnected(Context context){
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] infos = cm.getAllNetworkInfo();
for(NetworkInfo info:infos){
if(info.isAvailable()&&info.isConnected()){
return true;
}
}
return false;
}
}
NetworkInfo的isAvailable()和isConnected()有以下5种状态:
在WLAN设置界面
1,显示连接已保存,但标题栏没有,即没有实质连接上:isConnected()==false,isAvailable()=true
2,显示连接已保存,标题栏也有已连接上的图标:isConnected()==true,isAvailable()=true
3,选择不保存后:isConnected()==false,isAvailable()=true
4,选择连接,在正在获取IP地址时:isConnected()==false,isAvailable()=false
5,连接上后:isConnected()==true,isAvailable()=true
❹ 如何判断android手机当前是否联网
下面的方法返回false表示网络不通
// 检测网络
public static boolean checkNetworkAvailable(Context context) {
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[i].getState() == NetworkInfo.State.CONNECTED) {
NetworkInfo netWorkInfo = info[i];
if (netWorkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
return true;
} else if (netWorkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
return true;
}
}
}
}
}
return false;
}
❺ android 能判断当前网络是否连接,连接的是 wifi 还是 3g/4g
// 获取代表联网状态的NetWorkInfo对象 NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
// 获取当前的网络连接是否可用 if (null == networkInfo){Toast.makeText(this, "当前的网络连接不可用", Toast.LENGTH_SHORT).show();
//当网络不可用时,跳转到网络设置页面 startActivityForResult(new Intent(
android.provider.Settings.ACTION_WIRELESS_SETTINGS), 1);} else {boolean available = networkInfo.isAvailable();
if (available){Log.i("通知", "当前的网络连接可用");
Toast.makeText(this, "当前的网络连接可用", Toast.LENGTH_SHORT).show();} else {Log.i("通知", "当前的网络连接不可用");
Toast.makeText(this, "当前的网络连接不可用", Toast.LENGTH_SHORT).show();}}State state = connManager.getNetworkInfo(
ConnectivityManager.TYPE_MOBILE).getState();
if (State.CONNECTED == state){Log.i("通知", "GPRS网络已连接");
Toast.makeText(this, "GPRS网络已连接", Toast.LENGTH_SHORT).show();}state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.getState();
if (State.CONNECTED == state){Log.i("通知", "WIFI网络已连接");
Toast.makeText(this, "WIFI网络已连接", Toast.LENGTH_SHORT).show();}//// 跳转到无线网络设置界面
// startActivity(new
// Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
//// 跳转到无限wifi网络设置界面
// startActivity(new
// Intent(android.provider.Settings.ACTION_WIFI_SETTINGS)); }也可以:
privatevoid testConnectivityManager(){ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
//mobile 3G Network State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
Log.d(mobile.toString());
//wifi Network State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
Log.d(wifi.toString());
//如果3G网络和wifi网络都未连接,且不是处于正在连接状态 则进入Network Setting界面 由用户配置网络连接if(mobile==State.CONNECTED||mobile==State.CONNECTING)return;if(wifi==State.CONNECTED||wifi==State.CONNECTING)return;startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));//进入无线网络配置界面
//startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS)); //进入手机中的wifi网络设置界面 }
❻ android如何实现实时获取当前网络带宽
android如何实现实时获取当前网络带宽
/**
* 检查当前网络是否可用
*
* @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;
}
❼ android中如何判断当前信号是4G信号
通过Context.CONNECTIVITY_SERVICE获取系统服务(ConnectivityManager),通过ConnectivityManager.this.getActiveNetworkInfo()获取网络信息(NetworkInfo),调用NetworkInfo.this.getType()于ConnectivityManager的属性TYPE_MOBILE、TYPE_WIFI等做比较
❽ android 如何判断网络是否能够上网
实现步骤:
下面解决办法来自于android学习手册,android学习手册包含9个章节,108个例子,源码文档随便看,例子都是可交互,可运行,源码采用android studio目录结构,高亮显示代码,文档都采用文档结构图显示,可以快速定位。360手机助手中下载。排到第三个。
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;
}
}
❾ android 如何判断当前网络比如说是WI-FI还是移动网络 在线等啊
安卓系统的手机么? 一般都会在屏幕最上面,就是显示电池上面那里会有图标。如果是WLAN的话 它就会显示无线的图标。移动网络就是一个E字!!!