如何在Android上以编程方式启用或禁用GPS?

我知道关于在an​​droid 上 以编程方式打开/closuresGPS的问题已经 讨论过 很多 次了 ,答案总是一样的:

“你不能出于安全/隐私的原因,你必须转发到位置偏好屏幕,并让用户启用/禁用它。

据我所知,不过最近我从市场上购买了Tasker ,而且还可以用它来完成许多其他的事情,你可以设置规则来在进入预定的应用程序时自动启用GPS,并在退出时禁用它(参见这里教程如何做到这一点,它只是工作!),这个应用程序不能用固件签名密钥签名,因为它适用于许多Android版本和不同的设备,你甚至不需要根植。

我想在我的应用程序中做到这一点。 当然,我不想炸掉用户的隐私,所以我首先会询问用户是否想用典型的“记住我的决定”checkbox自动打开它,如果他回答是,启用它。

有没有人有任何想法或线索塔斯克如何实现这一目标?

GPS可以通过利用电源pipe理器小部件中的错误来切换。 看到这个xda线程的讨论。

这里是我使用的一些示例代码

private void turnGPSOn(){ String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if(!provider.contains("gps")){ //if gps is disabled final Intent poke = new Intent(); poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); poke.addCategory(Intent.CATEGORY_ALTERNATIVE); poke.setData(Uri.parse("3")); sendBroadcast(poke); } } private void turnGPSOff(){ String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if(provider.contains("gps")){ //if gps is enabled final Intent poke = new Intent(); poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); poke.addCategory(Intent.CATEGORY_ALTERNATIVE); poke.setData(Uri.parse("3")); sendBroadcast(poke); } } 

使用以下内容来testing电源控制部件的现有版本是否允许您切换GPS。

 private boolean canToggleGPS() { PackageManager pacman = getPackageManager(); PackageInfo pacInfo = null; try { pacInfo = pacman.getPackageInfo("com.android.settings", PackageManager.GET_RECEIVERS); } catch (NameNotFoundException e) { return false; //package not found } if(pacInfo != null){ for(ActivityInfo actInfo : pacInfo.receivers){ //test if recevier is exported. if so, we can toggle GPS. if(actInfo.name.equals("com.android.settings.widget.SettingsAppWidgetProvider") && actInfo.exported){ return true; } } } return false; //default } 

启用GPS:

 Intent intent=new Intent("android.location.GPS_ENABLED_CHANGE"); intent.putExtra("enabled", true); sendBroadcast(intent); 

禁用GPS:

 Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE"); intent.putExtra("enabled", false); sendBroadcast(intent); 

所有这些答案现在都是不允许的。 这是正确的:

对于所有仍在寻找答案的人:

以下是OLA驾驶室和其他类似的应用程序正在做的事情。

把这添加到你的onCreate中

 if (googleApiClient == null) { googleApiClient = new GoogleApiClient.Builder(this) .addApi(LocationServices.API).addConnectionCallbacks(this) .addOnConnectionFailedListener(Login.this).build(); googleApiClient.connect(); LocationRequest locationRequest = LocationRequest.create(); locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); locationRequest.setInterval(30 * 1000); locationRequest.setFastestInterval(5 * 1000); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() .addLocationRequest(locationRequest); // ************************** builder.setAlwaysShow(true); // this is the key ingredient // ************************** PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi .checkLocationSettings(googleApiClient, builder.build()); result.setResultCallback(new ResultCallback<LocationSettingsResult>() { @Override public void onResult(LocationSettingsResult result) { final Status status = result.getStatus(); final LocationSettingsStates state = result .getLocationSettingsStates(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: // All location settings are satisfied. The client can // initialize location // requests here. break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: // Location settings are not satisfied. But could be // fixed by showing the user // a dialog. try { // Show the dialog by calling // startResolutionForResult(), // and check the result in onActivityResult(). status.startResolutionForResult(Login.this, 1000); } catch (IntentSender.SendIntentException e) { // Ignore the error. } break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: // Location settings are not satisfied. However, we have // no way to fix the // settings so we won't show the dialog. break; } } }); } 

这些是实施的方法:

 @Override public void onConnected(Bundle arg0) { // TODO Auto-generated method stub } @Override public void onConnectionSuspended(int arg0) { // TODO Auto-generated method stub } @Override public void onConnectionFailed(ConnectionResult arg0) { // TODO Auto-generated method stub } 

这里是Android文档相同。

这是为了帮助其他人,如果他们还在挣扎:

编辑添加Irfan Raza的评论为更多的帮助。

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 1000) { if(resultCode == Activity.RESULT_OK){ String result=data.getStringExtra("result"); } if (resultCode == Activity.RESULT_CANCELED) { //Write your code if there's no result } } } 

如果应用程序移动到 /system/aps此代码在ROOTED电话上工作, 并且在清单中具有以下权限

 <uses-permission android:name="android.permission.WRITE_SETTINGS"/> <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/> 

 private void turnGpsOn (Context context) { beforeEnable = Settings.Secure.getString (context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); String newSet = String.format ("%s,%s", beforeEnable, LocationManager.GPS_PROVIDER); try { Settings.Secure.putString (context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED, newSet); } catch(Exception e) {} } private void turnGpsOff (Context context) { if (null == beforeEnable) { String str = Settings.Secure.getString (context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if (null == str) { str = ""; } else { String[] list = str.split (","); str = ""; int j = 0; for (int i = 0; i < list.length; i++) { if (!list[i].equals (LocationManager.GPS_PROVIDER)) { if (j > 0) { str += ","; } str += list[i]; j++; } } beforeEnable = str; } } try { Settings.Secure.putString (context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED, beforeEnable); } catch(Exception e) {} } 

从Android版本4.4开始,您无法以编程方式启用/禁用gps。 如果你尝试在这个答案上提出的代码,将会触发exception。

 java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.location.GPS_ENABLED_CHANGE 

而不是使用意图Settings.ACTION_LOCATION_SOURCE_SETTINGS你可以直接能够显示popup在你的应用程序,如谷歌地图和GPS上点击确定button,他们是没有必要redirect设置只需要使用我的代码作为

注意:如果Location不在,这行代码自动打开对话框。 这条线也被用在Google Map中

  public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { LocationRequest mLocationRequest; GoogleApiClient mGoogleApiClient; PendingResult<LocationSettingsResult> result; final static int REQUEST_LOCATION = 199; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(LocationServices.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this).build(); mGoogleApiClient.connect(); } @Override public void onConnected(Bundle bundle) { mLocationRequest = LocationRequest.create(); mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); mLocationRequest.setInterval(30 * 1000); mLocationRequest.setFastestInterval(5 * 1000); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() .addLocationRequest(mLocationRequest); builder.setAlwaysShow(true); result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build()); result.setResultCallback(new ResultCallback<LocationSettingsResult>() { @Override public void onResult(LocationSettingsResult result) { final Status status = result.getStatus(); //final LocationSettingsStates state = result.getLocationSettingsStates(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: // All location settings are satisfied. The client can initialize location // requests here. //... break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: // Location settings are not satisfied. But could be fixed by showing the user // a dialog. try { // Show the dialog by calling startResolutionForResult(), // and check the result in onActivityResult(). status.startResolutionForResult( MainActivity.this, REQUEST_LOCATION); } catch (SendIntentException e) { // Ignore the error. } break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: // Location settings are not satisfied. However, we have no way to fix the // settings so we won't show the dialog. //... break; } } }); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d("onActivityResult()", Integer.toString(resultCode)); //final LocationSettingsStates states = LocationSettingsStates.fromIntent(data); switch (requestCode) { case REQUEST_LOCATION: switch (resultCode) { case Activity.RESULT_OK: { // All required changes were successfully made Toast.makeText(MainActivity.this, "Location enabled by user!", Toast.LENGTH_LONG).show(); break; } case Activity.RESULT_CANCELED: { // The user was asked to change settings, but chose not to Toast.makeText(MainActivity.this, "Location not enabled, user cancelled.", Toast.LENGTH_LONG).show(); break; } default: { break; } } break; } } @Override public void onConnectionSuspended(int i) { } @Override public void onConnectionFailed(ConnectionResult connectionResult) { } 

}

注意:如果Location不在,这行代码会自动打开对话框。 这条线也被用在Google Map中

要以编程方式打开或closuresGPS,您需要安装“root”访问和BusyBox。 即使这样,任务也不是微不足道的。

示例在这里: Google Drive , Github , Sourceforge

testing了2.3.5和4.1.2 Android。

另一个问题的答案是,但它已经closures了,我希望社区也尝试一下。

 boolean gpsStatus = locmanager.isProviderEnabled(LocationManager.GPS_PROVIDER); if (!gpsStatus) { Settings.Secure.putString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "network,gps"); } 

看到这个评论

该解决scheme需要WRITE_SETTINGSWRITE_SECURE_SETTINGS权限。

也许用围绕类android.server.LocationManagerServicereflection技巧。

此外,还有一个方法(自API 8) android.provider.Settings.Secure.setLocationProviderEnabled

自从发布这个问题以来,情况发生了变化,现在使用新的Google Services API,您可以提示用户启用GPS:

https://developers.google.com/places/android-api/current-place

您需要在清单中请求ACCESS_FINE_LOCATION权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

也看这个video:

https://www.youtube.com/watch?v=F0Kh_RnSM0w

此代码适用于ROOTED手机:

 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String[] cmds = {"cd /system/bin" ,"settings put secure location_providers_allowed +gps"}; try { Process p = Runtime.getRuntime().exec("su"); DataOutputStream os = new DataOutputStream(p.getOutputStream()); for (String tmpCmd : cmds) { os.writeBytes(tmpCmd + "\n"); } os.writeBytes("exit\n"); os.flush(); } catch (IOException e){ e.printStackTrace(); } } } 

这是Google Developers提供的最佳解决scheme。 在初始化GoogleApiClient后,只需在onCreate的onResume中调用此方法GoogleApiClient

 private void updateMarkers() { if (mMap == null) { return; } if (mLocationPermissionGranted) { // Get the businesses and other points of interest located // nearest to the device's current location. LocationSettingsRequest.Builder builder = new LocationSettingsRequest .Builder() .addLocationRequest(mLocationRequest); PendingResult<LocationSettingsResult> resultPendingResult = LocationServices .SettingsApi .checkLocationSettings(mGoogleApiClient, builder.build()); resultPendingResult.setResultCallback(new ResultCallback<LocationSettingsResult>() { @Override public void onResult(@NonNull LocationSettingsResult locationSettingsResult) { final Status status = locationSettingsResult.getStatus(); final LocationSettingsStates locationSettingsStates = locationSettingsResult.getLocationSettingsStates(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: // All location settings are satisfied. The client can // initialize location requests here. break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: // Location settings are not satisfied, but this can be fixed // by showing the user a dialog. try { // Show the dialog by calling startResolutionForResult(), // and check the result in onActivityResult(). status.startResolutionForResult( MainActivity.this, PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION); } catch (IntentSender.SendIntentException e) { // Ignore the error. } break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: // Location settings are not satisfied. However, we have no way // to fix the settings so we won't show the dialog. break; } } }); @SuppressWarnings("MissingPermission") PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi .getCurrentPlace(mGoogleApiClient, null); result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() { @Override public void onResult(@NonNull PlaceLikelihoodBuffer likelyPlaces) { for (PlaceLikelihood placeLikelihood : likelyPlaces) { // Add a marker for each place near the device's current location, with an // info window showing place information. String attributions = (String) placeLikelihood.getPlace().getAttributions(); String snippet = (String) placeLikelihood.getPlace().getAddress(); if (attributions != null) { snippet = snippet + "\n" + attributions; } mMap.addMarker(new MarkerOptions() .position(placeLikelihood.getPlace().getLatLng()) .title((String) placeLikelihood.getPlace().getName()) .snippet(snippet)); } // Release the place likelihood buffer. likelyPlaces.release(); } }); } else { mMap.addMarker(new MarkerOptions() .position(mDefaultLocation) .title(getString(R.string.default_info_title)) .snippet(getString(R.string.default_info_snippet))); } } 

注意:如果Location不在,这行代码自动打开对话框。 这条线也被用在Google Map中

  status.startResolutionForResult( MainActivity.this, PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);