如何在Android App上以编程方式打开GPS

时间:2015-09-17 08:14:48

标签: android performance android-layout xamarin gps

请帮我在我的手机应用程序中打开GPS,如OlaCabs .. 我使用下面的代码:

Parse.initialize("appid", "jsid");
var list_item=Parse.Object.extend("className");
var query=new Parse.Query(list_item);

if (Meteor.isClient) {
  list = new Mongo.Collection(null);
  query.find({
    success:function(o){
      for (var i = 0; i < o.length; i++) {
        var object = o[i];
        list.insert(object.get('name'));

      }

    },
    error: function(error) {
      alert("Error: " + error.code + " " + error.message);
    }

  });
  Template.header.helpers({
     alphabets : function() {
      return list.find({});
     }
  });
}
</template>

但它无法启用GPS ..

3 个答案:

答案 0 :(得分:2)

Android指南已更改为4.0以上版本。对于4.0以上的版本,您无法以编程方式更改GPS ...

不要像我一样浪费你的时间

使用对话框检查:

https://developers.google.com/android/reference/com/google/android/gms/location/SettingsApi

或那:

https://stackoverflow.com/a/12446361/3626214

答案 1 :(得分:1)

对于那些仍在寻找答案的人:

以下是OLA Cabs和其他类似应用程序的用法。

在你的onCreate

中添加它
if (googleApiClient == null) {
            googleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(LocationServices.API).addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(Login.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:
                        // All location settings are satisfied. The client can
                        // initialize location
                        // requests here.
                        break;
                    case LocationSettingsStatusCodes.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(Login.this, 1000);
                        } catch (IntentSender.SendIntentException e) {
                            // Ignore the error.
                        }
                        break;
                    case LocationSettingsStatusCodes.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;
                    }
                }
            });
        }

这些是实施方法:

@Override
    public void onConnected(Bundle arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onConnectionSuspended(int arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onConnectionFailed(ConnectionResult arg0) {
        // TODO Auto-generated method stub

    }

答案 2 :(得分:0)

当手动启用位置时,您可以向用户显示一个对话框并将他/她重定向到设置