在Android 2.2以后才可使用

 import android.content.ContentResolver;
import android.content.Context;
import android.location.LocationManager;
import android.provider.Settings; /**
* GPS类,用来判断GPS是否开启,切换GPS状态
* 在 AndroidManifest.xml中需要添加权限:
* <uses-permission android:name="android.permission.WRITE_SETTINGS" />
* <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
* 在 AndroidManifest.xml中添加系统权限: android:sharedUserId="android.uid.system"
* 例如:
* <manifest xmlns:android="http://schemas.android.com/apk/res/android"
* android:sharedUserId="android.uid.system" >
* @author ljp 2014-1-11
*
*/
public class GPS {
/**
* Gets the state of GPS location.
*
* @param context
* @return true if enabled.
*/
public static boolean getGpsState(Context context) {
ContentResolver resolver = context.getContentResolver();
boolean open = Settings.Secure.isLocationProviderEnabled(resolver,
LocationManager.GPS_PROVIDER);
System.out.println("getGpsState:" + open);
return open;
} /**
* Toggles the state of GPS.
*
* @param context
*/
public static void toggleGps(Context context) {
ContentResolver resolver = context.getContentResolver();
boolean enabled = getGpsState(context);
Settings.Secure.setLocationProviderEnabled(resolver,
LocationManager.GPS_PROVIDER, !enabled);
}
}

如果加入权限 <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />后出现

permission is only granted to system apps问题,可以用以下解决方法:点eclipse的菜单project->clean

上面步骤后本来以为可以了,但又出现新的问题,那就是android.uid.system权限添加后编译出现错误:Installation error: INSTALL_FAILED_SHARED_USER_INCOMPATIBLE

解决办法:通过在linux环境下更改编码,vold 模块里的 Volume.cpp文件 在调用doMount的语句里做一下修改~ doMount(devicePath, path, false, false, false,1000, 1015, 0702, true) ↓ doMount(devicePath, path, false, true, false,1000, 1015, 0002, true) 编译以后.(由于没用过linux环境,暂时还没哟测试,待有空下个源码重编译一下)

最新文章

  1. Sql Server系列:运算符和表达式
  2. Database 'xxxx' is being recovered. Waiting until recovery is finished.
  3. AWK命令学习
  4. UICollectionReusableView 使用时的一些问题
  5. SQL疑难杂症【5 】大量数据查询的时候要考虑结果为空的情况
  6. css关系选择器
  7. 项目回顾1-图片上传-form表单还是base64-前端图片压缩
  8. JAVA HttpsURLConnection 忽略对SSL valid 的验证
  9. ajax详解,以及异步JSOP的实现
  10. 各种Markdown处理器的列表
  11. 阻止文件不被上传到iCloud-b
  12. 017_bcp_bulk_openrowset
  13. iOS 程序测试、程序优化、提交前检测
  14. &lt;jsp:include&gt;和&lt;%@include%&gt;的区别
  15. CRM客户关系管理系统(五)
  16. mac的vscode配置使用zsh
  17. 第18月第19天 masonry等分 uilabel sizetofit
  18. TextBox Ctrl+A不能全选的问题
  19. HDU3605(KB11-M 状态压缩+最大流)
  20. Mybatis mark 勿看

热门文章

  1. Leetcode 52 N-Queens II 回溯搜索
  2. Struts2入门1 Struts2基础知识
  3. js 停止事件冒泡 阻止浏览器的默认行为(阻止超连接 # )
  4. Activiti 流程实例、任务、执行对象及相关的表
  5. Redis安装及HA(High Availability)配置
  6. redis一句话
  7. Python中is和==的区别
  8. hive中grouping sets的使用
  9. 文件系统管理 之 Linux 查看磁盘分区、文件系统、使用情况的命令和相关工具介绍
  10. ECSHOP始终显示全部分类方法