开启Android程序时检测网络与GPS是否打开
2016-07-13 11:25
417 查看
很多android程序在打开时,都需要检测网络是否连接,或者GPS是否可用:
1.网络是否连接(包括Wifi和移动网络)
[javascript] view
plain copy
print?
// 是否有可用网络
private boolean isNetworkConnected() {
ConnectivityManager cm =
(ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo network = cm.getActiveNetworkInfo();
if (network != null) {
return network.isAvailable();
}
return false;
}
![](http://static.blog.csdn.net/images/save_snippets.png)
2.wifi是否可用
[javascript] view
plain copy
print?
// Wifi是否可用
private boolean isWifiEnable() {
WifiManager wifiManager = (WifiManager) mContext
.getSystemService(Context.WIFI_SERVICE);
return wifiManager.isWifiEnabled();
}
3.GPS是否可用
[javascript] view
plain copy
print?
// Gps是否可用
private boolean isGpsEnable() {
LocationManager locationManager =
((LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE));
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
转载地址:http://blog.csdn.net/sky837/article/details/7867601
1.网络是否连接(包括Wifi和移动网络)
[javascript] view
plain copy
print?
// 是否有可用网络
private boolean isNetworkConnected() {
ConnectivityManager cm =
(ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo network = cm.getActiveNetworkInfo();
if (network != null) {
return network.isAvailable();
}
return false;
}
![](http://static.blog.csdn.net/images/save_snippets.png)
2.wifi是否可用
[javascript] view
plain copy
print?
// Wifi是否可用
private boolean isWifiEnable() {
WifiManager wifiManager = (WifiManager) mContext
.getSystemService(Context.WIFI_SERVICE);
return wifiManager.isWifiEnabled();
}
3.GPS是否可用
[javascript] view
plain copy
print?
// Gps是否可用
private boolean isGpsEnable() {
LocationManager locationManager =
((LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE));
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
转载地址:http://blog.csdn.net/sky837/article/details/7867601
相关文章推荐
- linux网络故障解决方法
- UNIX网络编程——并发服务器(多线程)
- 无法创建XMLHTTP对象,请检查是否安装了MS XML Parser运行库
- 《大白书》192页 uva 1329 合作网络
- Android第三方HTTP网络支持包OkHttp的基础使用教程
- The absolute uri: http://java.sun.com/jsp/jstl/fmt cannot be resolved in either web.xml or the jar
- eclipse提示Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/functions"
- Tomcat 支持https请求的设置
- window下单机最大tcp连接数
- Android App开发中HTTP扩展包OkHttp的入门使用指南
- spark streaming 读取网络数据
- caffe学习系列:绘制网络模型
- Android中使用OkHttp包处理HTTP的get和post请求的方法
- 必须掌握的八个DOS网络命令
- HttpClent请求
- HTTP协议在前端开发中的应用
- 谈谈网络请求
- 网络知识
- One Step By One Step 解析OkHttp3 - Dispatcher (一)
- okhttp,retrofit,android-async-http,volley应该选择哪一个?