Android通过编码实现GPS开关
2024-08-27 05:13:22
在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环境,暂时还没哟测试,待有空下个源码重编译一下)
最新文章
- Sql Server系列:运算符和表达式
- Database 'xxxx' is being recovered. Waiting until recovery is finished.
- AWK命令学习
- UICollectionReusableView 使用时的一些问题
- SQL疑难杂症【5 】大量数据查询的时候要考虑结果为空的情况
- css关系选择器
- 项目回顾1-图片上传-form表单还是base64-前端图片压缩
- JAVA HttpsURLConnection 忽略对SSL valid 的验证
- ajax详解,以及异步JSOP的实现
- 各种Markdown处理器的列表
- 阻止文件不被上传到iCloud-b
- 017_bcp_bulk_openrowset
- iOS 程序测试、程序优化、提交前检测
- <;jsp:include>;和<;%@include%>;的区别
- CRM客户关系管理系统(五)
- mac的vscode配置使用zsh
- 第18月第19天 masonry等分 uilabel sizetofit
- TextBox Ctrl+A不能全选的问题
- HDU3605(KB11-M 状态压缩+最大流)
- Mybatis mark 勿看