GPS无法在我的android应用程序中运行,但可以在Google Maps中运行

时间:2018-06-27 14:24:15

标签: android google-maps google-api gps location

enter image description here我的Android应用程序出现问题,应用程序中的GPS不能正常运行,但是当我运行Google地图并再次运行时,它运行良好。我不知道原因

public void getDeviceLocation(){

    mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);

    try{

    final Task location = mFusedLocationProviderClient.getLastLocation();

        location.addOnCompleteListener(new OnCompleteListener() {
            @Override
            public void onComplete(@NonNull Task task) {

                if(task.isSuccessful() && task.getResult() != null){

                   Location currentLocation = (Location) task.getResult();


                    longitude = currentLocation.getLongitude();
                    latitude  = currentLocation.getLatitude();



                }else{

                    Toast.makeText(MapsActivity.this,"unable to get your current location, please refresh the page or check your internet access is good",Toast.LENGTH_SHORT).show();

                }

            }
        });

    }catch (SecurityException e){
        Log.e(TAG,"Security exception: " + e.getMessage());
    }
}

在清单中:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

这是请求打开GPS的方法,此代码会弹出一个对话框,告诉他是否接受打开GPS

private void displayLocationSettingsRequest(Context context) {

    GoogleApiClient googleApiClient = null;
    if (googleApiClient == null) {
        googleApiClient = new GoogleApiClient.Builder(context)
                .addApi(LocationServices.API).addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this).build();
        googleApiClient.connect();
        LocationRequest locationRequest = LocationRequest.create();

         locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(5 * 1000);
        locationRequest.setFastestInterval(5 * 1000 / 2);
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);


        builder.setAlwaysShow(true); 

        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:
                        toast("Success");
                        // All location settings are satisfied. The client can
                        // initialize location
                        // requests here.
                        break;
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        toast("GPS is not on");
                        // 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(decision.this, 1000);

                        } catch (IntentSender.SendIntentException e) {
                            // Ignore the error.
                        }
                        break;
                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                        toast("Setting change not allowed");
                        // Location settings are not satisfied. However, we have
                        // no way to fix the
                        // settings so we won't show the dialog.
                        break;
                }
            }
        });
    }
}

0 个答案:

没有答案