Android通过编码实现GPS开关

在Android 2.2以后才可使用

 1 import android.content.ContentResolver;
 2 import android.content.Context;
 3 import android.location.LocationManager;
 4 import android.provider.Settings;
 5 
 6 /**
 7  * GPS类,用来判断GPS是否开启,切换GPS状态
 8  * 在 AndroidManifest.xml中需要添加权限:
 9  * <uses-permission android:name="android.permission.WRITE_SETTINGS" />
10  * <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
11  * 在 AndroidManifest.xml中添加系统权限: android:sharedUserId="android.uid.system" 
12  * 例如:
13  * <manifest xmlns:android="http://schemas.android.com/apk/res/android"
14  *     android:sharedUserId="android.uid.system" >
15  * @author ljp 2014-1-11
16  *
17  */
18 public class GPS {
19     /**
20      * Gets the state of GPS location.
21      * 
22      * @param context
23      * @return true if enabled.
24      */
25     public static boolean getGpsState(Context context) {
26         ContentResolver resolver = context.getContentResolver();
27         boolean open = Settings.Secure.isLocationProviderEnabled(resolver,
28                 LocationManager.GPS_PROVIDER);
29         System.out.println("getGpsState:" + open);
30         return open;
31     }
32 
33     /**
34      * Toggles the state of GPS.
35      * 
36      * @param context
37      */
38     public static void toggleGps(Context context) {
39         ContentResolver resolver = context.getContentResolver();
40         boolean enabled = getGpsState(context);
41         Settings.Secure.setLocationProviderEnabled(resolver,
42                 LocationManager.GPS_PROVIDER, !enabled);
43     }
44 }

 

如果加入权限 <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环境,暂时还没哟测试,待有空下个源码重编译一下

转载于:https://www.cnblogs.com/gisdream/p/3515389.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值