判断GPS、网络是否开启

1.判断GPS打开与否,没有打开则打开GPS

private void initGPS(Context context) {
LocationManager locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
// 判断GPS模块是否开启,如果没有则开启
if (!locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage("请打开GPS");
dialog.setPositiveButton("确定", new android.content.DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface arg0, int arg1) { // 转到手机设置界面,用户设置GPS
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, 0); // 设置完成后返回到原来的界面
}
});
dialog.setNeutralButton("取消", new android.content.DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface arg0, int arg1) {
arg0.dismiss();
}
});
dialog.show();
}
}

判断网络是否开启

private void initInternet(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
boolean isConnect = cm.getActiveNetworkInfo().isAvailable();
if (!isConnect) {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage("请打开网络或连接wifi");
dialog.setPositiveButton("确定", new android.content.DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface arg0, int arg1) { // 转到手机设置界面,用户设置GPS
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, 0); // 设置完成后返回到原来的界面 }
});
dialog.setNeutralButton("取消", new android.content.DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface arg0, int arg1) {
arg0.dismiss();
}
});
dialog.show();
}
}

判断GPS、网络是否开启的更多相关文章

  1. Android判断GPS是否开启和强制帮用户打开GPS

    引子:在我们的应用为用户提供定位服务时,通常想为用户提供精确点的定位服务,这是需要用户配合的.我们必须先检测用户手机的GPS当前是否打开,若没打开则弹出对话框提示.用户若不配合我们也没办法,只能采用基 ...

  2. 判断GPS是否开启&转到设置GPS界面

    /** * 判断GPS是否开启,GPS或者AGPS开启一个就认为是开启的 * @param context * @return true 表示开启 */ public static final boo ...

  3. 判断网络是否连接 和 判断GPS是否连接

    //判断网络是否连接 public static Boolean isNetworkEnabled(Context context){ int status=-1  //设置默认连接的状态为-1 Co ...

  4. Android 判断wifi和4G网络是否开启

    public boolean isWifiAvailable() { ConnectivityManager connectivityManager = (ConnectivityManager) g ...

  5. XAMARIN +VS2015 ANDROID 开发判断gps 是否打开。

    在获取位置的时候首先要判断gps是否打开,如果没有打开就要提示打开,当然最友好的就是直接调转到打开界面. LocationManager alm = (LocationManager)this.Get ...

  6. Android中判断当前网络是否可用

    转载原文地址:http://www.cnblogs.com/renqingping/archive/2012/10/18/Net.html 当前有可用网络,如下图: 当前没有可用网络,如下图: 实现步 ...

  7. iOS  如何判断当前网络连接状态  网络是否正常  网络是否可用

    网络资源:出处http://blog.csdn.net/mad1989/article/details/8987368   众所周知,我们在开发APP时,涉及网络连接的时候,都会想着提前判断一下当前的 ...

  8. api.connectionType 判断当前网络技术经验

    使用  api.connectionType 判断当前网络的时候,需要注意,要加入大小写转换,三星返回的网络是大写 3G /** * 返回当前是否联网 * 周枫 * 3g 4g wifi none * ...

  9. Android 判断当前网络连接类型

    实际应用开发时,如果存在需要用户获取大量数据的情况,最好是先判断下网络类型,提示用户当前的网络类型,是否需要连接Wifi,etc.(手机流量太贵啦,当然土豪是无视这玩意的, (/ □ \)). 定义网 ...

随机推荐

  1. 0511 backlog 项目管理

    SCRUM 这次的作业就是确定SCRUM的计划,确定sprint backlog的一个冲刺周期,而这个周期是两个星期.争取在两周内发布1.0版本. 本次作业以网站构建为主: ID       NAME ...

  2. 魔兽塔防游戏android源码

    魔兽塔防是一款经典的游戏,当年在pc机器上玩过魔兽的人应该都玩过类似的游戏,他仿照魔兽,建塔拦截敌人入侵,发挥你的智慧让敌人走最远的路,将他们消灭在路上.... 源码下载:http://code.66 ...

  3. wget下载FTP的文件

    在Linux中我们怎么样实现wget来下载文件  下面例子 下载所有的py结尾的文件到当前目录 wget ftp://anymous:anymous@42.51.152.2/soft/*.py 递归的 ...

  4. Hbase Interface HConnection

    HTablePool 在Hbase 0.94.0.95.0.97被废弃,在0.98中被清除( HTablePool 对比HConnection.getTable),hbase0.98 HTablePo ...

  5. PHP mysql 事务处理实例

    事务是必须满足4个条件(ACID):原子性(Autmic).一致性(Consistency).隔离性(Isolation).持久性(Durability) 原子性(Autmic):事务在执行性,要做到 ...

  6. DevExpress 中 在做全选的全消功能的时候 加快效率

    在做 DevExpress 中对增加的选择 Check列 控制全选的全消时通过以下代码红色字代码效率会有明显的提升: private void CheckedRow() { try { splashS ...

  7. ubuntu miss tool bar

    reson: unity exception 1. open terminal:  /usr/bin/**terminal** 2. run command on terminal: gsetting ...

  8. Microsoft Azure 的一些限制 Global

    Azure Subscription and Service Limits, Quotas, and Constraints http://azure.microsoft.com/en-us/docu ...

  9. bat文件编写(无线承载网络设置)

    就弄个例子,自己看执行效果,然后模仿写就行. 1)获取当前时间: @echo off set YEAR=%date:~0,4% set MONTH=%date:~5,2% set DAY=%date: ...

  10. hdu 5443 The Water Problem

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5443 The Water Problem Description In Land waterless, ...