5

我想实施Enable/Disable mobile data and GPS settings. 我已经搜索了启用/禁用移动数据和 GPS 设置的可用 Android API,以下是我的发现。

启用/禁用移动数据 – 1. 可以在 <5.0 Android OS 中执行启用/禁用。2. Android OS 5.0+ 起,非系统应用尚无法启用/禁用移动数据。从 5.0 或更高版本开始,我们在执行相同操作时会遇到此异常——第三方应用程序不使用该异常。原因:java.lang.SecurityException: Neither user 10314 nor current process has android.permission.MODIFY_PHONE_STATE

是否有任何可能/可用的解决方案?

GPS 设置 - 1. 我们能够以编程方式打开位置,但尚未找到禁用它的方法(不包括 Intent 方法)。

任何人都知道如何在不通过意图移动到设置的情况下禁用 GPS(以编程方式定位)。

提前致谢。

4

2 回答 2

1

我们正在更改 GPS 设置,而不使用SettingsApi移动到设置屏幕

要检查 GPS 是打开还是关闭,您必须检查如下,

public class GPSActivity extends AppCompatActivity implements View.OnClickListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private static String TAG = "GPSActivity";

// Required for setting API
protected static final int REQUEST_CHECK_SETTINGS = 0x1;
GoogleApiClient googleApiClient;

private Button mBtnGPS;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gps);

    mBtnGPS = (Button) findViewById(R.id.btnGPS);

    mBtnGPS.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.btnGPS:
            // Check GPS
            checkGps();
            break;
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);
    if (requestCode == REQUEST_CHECK_SETTINGS) {
        googleApiClient = null;
        checkGps();
    }
}

public void checkGps() {
    if (googleApiClient == null) {
        googleApiClient = new GoogleApiClient.Builder(GPSActivity.this)
                .addApiIfAvailable(LocationServices.API)
                .addConnectionCallbacks(this).addOnConnectionFailedListener(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:
                        Log.i("GPS", "SUCCESS");
                        //getFbLogin();
                        break;
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        Log.i("GPS", "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(GPSActivity.this, REQUEST_CHECK_SETTINGS);
                        } catch (IntentSender.SendIntentException e) {
                            // Ignore the error.
                        }
                        break;
                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                        Log.i("GPS", "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;
                    case LocationSettingsStatusCodes.CANCELED:
                        Log.i("GPS", "CANCELED");
                        break;
                }
            }
        });
    }
}

@Override
public void onConnected(Bundle bundle) {

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

}

这是文档

于 2016-11-29T11:46:07.937 回答
0

这是从 GPS 和网络(Wi-Fi/数据)提供商启动定位服务的方法。

LocationManager locationManager = (LocationManager)getContext().getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);        
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,locationListener);

如果您想停止侦听位置更新,请运行以下代码: locationManager.removeUpdates(locationListener);

无论提供者(GPS/网络)如何,单行代码都会停止侦听任何位置更新,因为 LocationManager 不关心更新来自何处。

在您的情况下,我假设您知道如何创建一些 UI 来让用户决定是否使用 GPS/网络。然后,您可以执行以下操作:

if ( useGPS ) {
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
}
if ( useNetwork ) {
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,locationListener);
}

如果用户同时启用这两个提供商,则位置更新可能会更准确。如果用户禁用了这两个提供程序,也没关系。由于 LocationListener 不会有任何更新,它应该是用户想要的。

顺便说一下,这是创建 LocationListener 的代码:

LocationListener locationListener =  new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            // you may add some logic here to determine whether the new location update is more accurate than the previous one
            if ( isBetterLocation(location,currentBestLocation) ) {
                currentBestLocation = location;
            }
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onProviderDisabled(String provider) {
        }
    };
于 2017-06-12T23:55:47.057 回答