【问题标题】:Mobile data and GPS ON/OFF programatically - Android above 5.0 too以编程方式打开/关闭移动数据和 GPS - Android 也高于 5.0
【发布时间】:2016-11-29 09:06:19
【问题描述】:

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

启用/禁用移动数据 – 1. 可以在 java.lang.SecurityException:Neither user 10314 nor current process has android.permission.MODIFY_PHONE_STATE

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

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

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

提前致谢。

【问题讨论】:

  • 调用 removeLocationUpdates() 停止使用 GPS。
  • 您是否向应用中的用户请求运行时权限?
  • @ArpitPatel 我正在寻找一种在应用程序内执行它的方法。
  • 是否有任何可能/可用的解决方案?不......因为这个控件可能应该由用户保留。开发人员通常需要启用 GPS 才能使用它的权限,禁用 GPS 并不是不使用它的必要条件:)

标签: android gps 3g 4g


【解决方案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) {

}

}

这里是Documentation

【讨论】:

  • 我使用相同的代码启用。我正在寻找禁用它。如果你知道,请告诉我。
  • 可能是不可能的。因为在google官方文档中,我找不到。所以我们必须在设置屏幕中导航。 @Manmohan
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多