代码开启关闭Gps
在Android 2.2以后才可使用
/**
* Gets the state of GPS location.
*
* @param context
* @return true if enabled.
*/
private 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
*/
private 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_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"
>
哈哈,说的很对啊
是的, 如上面我所说
3楼不就是你自己么?
目前我发现的方式 只能跳转到GPS设置界面让用户手动开启, 直接在后台开启是需要系统签名的, 正如3楼
楼主你有真正实践过么?你的两段代码只是Android系统原生代码中的一部分!正如前面鸡楼所说的,问题根本就没有解决
Permission is only granted to system apps Permission is only granted to system apps Permission is only granted to system apps 不通过啊
加入android.uid.system之后需要签名
为什么我加了 两个权限,都是提示 10-19 04:44:51.613: ERROR/AndroidRuntime(445): Caused by: java.lang.SecurityException: Permission denial: writing to secure settings requires android.permission.WRITE_SECURE_SETTINGS 请问怎么回事? 最后加的那个结果提示Installation error: INSTALL_FAILED_SHARED_USER_INCOMPATIBLE错误。 那个是干什么的到底?
问题解决,谢谢