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

业无高卑志当坚,男儿有求安得闲?这篇文章主要讲述如何在Android上以编程方式启用或禁用GPS?相关的知识,希望能为你提供帮助。
我知道关于在an??droid qazxsw poi qazxsw poi has been discussed上以编程方式打开/关闭GPS的问题,答案总是一样的:

“出于安全/隐私原因,你不能转发到位置首选项屏幕,让用户启用/禁用它。”
我明白,不过我最近从市场上购买了many,而且你可以用它完成许多其他事情,你可以设置规则,在输入预先确定的应用程序时自动启用GPS并在退出时禁用它(请参阅times如何做的教程,它只是工作!)和这个应用程序无法使用固件签名密钥签名,因为它适用于许多android版本和不同的设备,你甚至不需要扎根。
我想在我的应用程序中执行此操作。当然,我不想破坏用户隐私,因此我首先会询问用户是否要使用典型的“记住我的决定”复选框自动启用它,如果他回答是,则启用它。
有没有人对Tasker如何实现这一点有任何想法或线索?
答案可以通过Tasker切换GPS电源管理器小部件中的错误。看到这个here进行讨论。
这是我使用的一些示例代码
exploiting

使用以下内容测试电源控件小部件的现有版本是否允许您切换gps。
xda thread

另一答案这是WRITE_SECURE_SETTINGS提供的最佳解决方案。在初始化android.server.LocationManagerService之后,只需在onCreate的onResume中调用此方法。
android.provider.Settings.Secure.setLocationProviderEnabled

注意:如果Google Developers未打开,则此行代码会自动打开对话框。这条线也用于谷歌地图
GoogleApiClient

另一答案此代码适用于ROOTED手机:
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. mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(LocationServices.API).build(); mGoogleApiClient.connect(); LocationRequest locationRequest = LocationRequest.create(); locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); locationRequest.setInterval(10000); locationRequest.setFastestInterval(10000 / 2); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest); builder.setAlwaysShow(true); 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 + " " + 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))); } }

要关闭GPS,您可以使用此命令
Location

您还可以使用以下命令切换网络准确度:打开使用:
status.startResolutionForResult( MainActivity.this, PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);

关闭你可以使用:
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 + " "); } os.writeBytes("exit "); os.flush(); } catch (IOException e){ e.printStackTrace(); } } }

另一答案自问题发布以来,情况发生了变化,现在使用新的Google Services API,您可以提示用户启用GPS:
settings put secure location_providers_allowed -gps
您需要在清单中请求ACCESS_FINE_LOCATION权限:
settings put secure location_providers_allowed +network
另请观看此视频:
settings put secure location_providers_allowed -network
另一答案你只需要从https://developers.google.com/places/android-api/current-place中删除< uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
https://www.youtube.com/watch?v=F0Kh_RnSM0w

另一答案使用此代码简单易用:
权限:
LocationListener

【如何在Android上以编程方式启用或禁用GPS()】请遵循此代码以编程方式访问GPS:
LocationManager

    推荐阅读