代码开启关闭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"
      >

你可能感兴趣的:(android,xml)