Android通过编码实现GPS开关
16lz
2021-12-04
在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-1116 *17 */18 public class GPS {19 /**20 * Gets the state of GPS location.21 * 22 * @param context23 * @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 context37 */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环境,暂时还没哟测试,待有空下个源码重编译一下)
更多相关文章
- 2011.09.09(2)——— android 桌面添加快捷方式
- 新版本Android(安卓)Souce 编译问题
- AndroidManifest.xml - 【 manifest -> permission】
- Android(安卓)studio 中调用ndk-build 进行编译native 层直接编
- Android(安卓)编译系统(一):Android.mk的学习
- Mainfest
- Android(安卓)存储方式之文件存储
- android 判断当前application 是在前台还是在后台
- Android(安卓)9.0 (P版本) SystemServer中的服务配置se linux权