Android 5.0以上版本如何通过程序控制开关移动数据和GPS?

5
我希望实现启用/禁用移动数据和GPS设置。我已经搜索了可用的Android API,以启用/禁用移动数据和GPS设置,并且以下是我的发现。
启用/禁用移动数据 - 1. 在小于5.0的Android操作系统中可以执行启用/禁用操作。 2. 从5.0+的Android操作系统开始,非系统应用程序尚不能启用/禁用移动数据。在5.0或更高版本中,我们在执行相同操作时会得到以下异常 - 不适用于第三方应用程序。 Caused by: java.lang.SecurityException: Neither user 10314 nor current process has android.permission.MODIFY_PHONE_STATE。
是否有可能/可用的解决方案?
GPS设置 - 1. 我们能够通过编程方式打开位置,但还没有找到禁用它的方法(不包括Intent方法)。
是否有人知道如何通过编程方式禁用GPS(而不通过意图转到设置)。
提前致谢。

调用removeLocationUpdates()停止使用GPS。 - Rushi Ayyappa
你的应用程序是否在请求用户运行时权限? - Arpit Patel
@ArpitPatel 我正在寻找一种在应用程序内执行它的方法。 - Manmohan Badaya
1
有可能/可用的解决方案吗?
没有。
...因为这个控制应该保留给用户。开发人员通常需要获得权限才能启用GPS并使用它,禁用GPS并不是不使用它所必需的 :)
- Pararth
2个回答

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) {

}

}

这里是文档

我已经使用了相同的代码来启用它。我现在正在寻找禁用它的方法。如果您知道,请告诉我。 - Manmohan Badaya
可能不太可能。因为在谷歌官方文档中,我找不到相关信息。所以我们必须在设置屏幕中进行导航。@Manmohan - Harsh Patel

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) {
        }
    };

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接