以编程方式显示移动数据和GPS ON/OFF - Android也高于5.0

Man*_*aya 5 gps android 3g 4g

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

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

有可能/可用的解决方案吗?

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

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

提前致谢.

Har*_*tel 1

我们正在使用SettingsApi更改 GPS 设置,而无需转到设置屏幕

要检查 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) {

}
Run Code Online (Sandbox Code Playgroud)

}

这是文档