以编程方式在Android上禁用GPS

时间:2015-11-23 06:39:43

标签: java android gps google-api-client

我正在尝试创建一个供我自己使用的应用,启用/禁用某些功能,如WiFi,GPS等;我的问题与GPS有关。请注意,我的手机没有扎根,我希望保持这种状态。

我已使用以下代码成功启用了GPS:

public void toggleGPS() {
    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
        .addApi(LocationServices.API)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .build();

    LocationRequest locationRequest = new LocationRequest();

    if (isGPSEnabled()) {
        locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        locationRequest.setInterval(60 * 60 * 1000);
        locationRequest.setFastestInterval(60 * 60 * 1000);
    } else {
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(1000);
        locationRequest.setFastestInterval(1000);
    }

    googleApiClient.connect();

    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);

    PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());

    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
        @Override
        public void onResult(LocationSettingsResult result) {
            Status status = result.getStatus();
            LocationSettingsStates locationSettingsStates = result.getLocationSettingsStates();

            if (status.getStatusCode() == LocationSettingsStatusCodes.RESOLUTION_REQUIRED) {
                try {
                    // Show the dialog by calling startResolutionForResult(),
                    // and check the result in onActivityResult().
                    status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
                } catch (IntentSender.SendIntentException e) {

                }
            }
        }
    });
}

因此,当GPS未打开时,此代码会提示我按照我的意愿将其打开。但是,当我请求Balanced Power并设置更大的间隔并且没有其他应用程序使用GPS时,它似乎没有关闭。我认为如果我的应用程序不再需要高精度,那么GPS将自动关闭。我误会了什么吗?有没有办法把它关掉?我可以理解为了禁止以编程方式启用GPS的安全问题,但我不明白为什么Android不允许它被禁用。

提前致谢!

1 个答案:

答案 0 :(得分:0)

试试这个代码它适用于所有版本,试一试....

// automatic turn off the gps
public void turnGPSOff()
{
    String provider = Settings.Secure.getString(ctx.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")); 
        this.ctx.sendBroadcast(poke);
    }
}

和要启用的代码如下,您可以使用此..

public void turnGPSOn()
{
     Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
     intent.putExtra("enabled", true);
     this.ctx.sendBroadcast(intent);

    String provider = Settings.Secure.getString(ctx.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")); 
        this.ctx.sendBroadcast(poke);


    }
}