【问题标题】:wait till the GPS is enabled before getting the lat and long value in FusedLocationAPI等到 GPS 启用,然后在 FusedLocationAPI 中获取 lat 和 long 值
【发布时间】:2018-03-03 21:06:29
【问题描述】:

我正在尝试在我的应用程序中使用 FusedLocationAPI 获取当前位置。它在 android API>23 和

onCreate() 内部:

         manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
     if (!statusOfGPS) {
                displayPromptForEnablingGPS(this);
            }

           Log.d("permipart", "before");

            if(CheckingPermissionIsEnabledOrNot())
            {
                Toast.makeText(MainActivity.this, "All Permissions Granted Successfully", Toast.LENGTH_LONG).show();
            }

            else {
                RequestMultiplePermission();    
            }

            if(Build.VERSION.SDK_INT<= 23)
            {
                buildGoogleApiClient();
                if (!mGoogleApiClient.isConnected()) {
                    mGoogleApiClient.connect();
                }
            }
            Log.d("permipart", "out");

其他方法:

       @Override
    public void onLocationChanged(Location location) {
        Log.d("onconnectedlocchange","called");
        if (location != null) {
            lat = location.getLatitude();
            lon = location.getLongitude();
            if (!String.valueOf(lat).equals("0.0"))
            {
                latitudeVal = location.getLatitude();
                longitudeVal = location.getLongitude();
                Log.e("Lat and Lng", String.valueOf(latitudeVal) + longitudeVal);
            }
        }

    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {

        if(CheckingPermissionIsEnabledOrNot())
        {
            Toast.makeText(MainActivity.this, "All Permissions Granted Successfully", Toast.LENGTH_LONG).show();
        }

        // If, If permission is not enabled then else condition will execute.
        else {

            //Calling method to enable permission.
            RequestMultiplePermission();

        }
        Log.d("onconnected","called");

        mLocationRequest = LocationRequest.create();
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        // Update location every second

        if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        }
        else
        {
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
            mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                    mGoogleApiClient);
            if (mLastLocation != null) {
                lat = mLastLocation.getLatitude();
                lon = mLastLocation.getLongitude();
                if (!String.valueOf(lat).equals("0.0")){
                    latitudeVal = mLastLocation.getLatitude();
                    longitudeVal = mLastLocation.getLongitude();
                    Log.e("Lat and Lng", String.valueOf(latitudeVal)+ longitudeVal);
                }
            }
        }


        }

        @Override
        public void onConnectionSuspended(int i) {

        }
        synchronized void buildGoogleApiClient() {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .build();
        }

        @Override
        public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

        }
        public void displayPromptForEnablingGPS(final Activity activity) {
            final android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(activity);
            final String action = Settings.ACTION_LOCATION_SOURCE_SETTINGS;
            final String message = "Do you want open GPS setting?";
            builder.setMessage(message)
                    .setPositiveButton("OK",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface d, int id) {
                                    activity.startActivity(new Intent(action));
                                    d.dismiss();
                                }
                            })
                    .setNegativeButton("Cancel",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface d, int id) {
                                    d.cancel();
                                }
                            });
            builder.create().show();
        }

    String[] permissions = new String[]{ACCESS_FINE_LOCATION,
            ACCESS_COARSE_LOCATION, READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE };


    //Permission function starts from here
    private void RequestMultiplePermission() {

        // Creating String Array with Permissions.
        Log.d("permipart", "RequestMultiplePermission");
        ActivityCompat.requestPermissions(MainActivity.this, new String[]
                {
                        READ_EXTERNAL_STORAGE,
                        WRITE_EXTERNAL_STORAGE,
                        ACCESS_FINE_LOCATION,
                        ACCESS_COARSE_LOCATION
                }, RequestPermissionCode);

    }

    private  boolean checkPermission() {
        int result;
        List<String> listPermissionsNeeded = new ArrayList<>();
        for (String p:permissions) {
            result = ContextCompat.checkSelfPermission(this,p);
            if (result != PackageManager.PERMISSION_GRANTED) {
                listPermissionsNeeded.add(p);
            }
        }
        if (!listPermissionsNeeded.isEmpty()) {
            ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),10 );

            Log.d("permissionissue","no");
            return false;
        }
        Log.d("permissionissue","yes");
        return true;
    }




    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {

        Log.d("permipart", "onRequestPermissionsResult");
        switch (requestCode) {


            case RequestPermissionCode:

                if (grantResults.length > 0) {

                    boolean CameraPermission = grantResults[0] == PackageManager.PERMISSION_GRANTED;
                    boolean RecordAudioPermission = grantResults[1] == PackageManager.PERMISSION_GRANTED;
                    boolean SendSMSPermission = grantResults[2] == PackageManager.PERMISSION_GRANTED;
                    boolean GetAccountsPermission = grantResults[3] == PackageManager.PERMISSION_GRANTED;

                    if (CameraPermission && RecordAudioPermission && SendSMSPermission && GetAccountsPermission) {

                        Log.d("permipart", "done");
                        buildGoogleApiClient();
                        Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_LONG).show();
                        if (!mGoogleApiClient.isConnected()) {
                            mGoogleApiClient.connect();
                        }
                    }
                    else {
                        Toast.makeText(MainActivity.this,"Permission Denied",Toast.LENGTH_LONG).show();

                    }
                }

                break;
        }
    }



    /**
     * Checking permission is enabled or not using function starts from here.
     *
     */
    public boolean CheckingPermissionIsEnabledOrNot() {

        int FirstPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), READ_EXTERNAL_STORAGE);
        int SecondPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), WRITE_EXTERNAL_STORAGE);
        int ThirdPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), ACCESS_FINE_LOCATION);
        int ForthPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), ACCESS_COARSE_LOCATION);

        return FirstPermissionResult == PackageManager.PERMISSION_GRANTED &&
                SecondPermissionResult == PackageManager.PERMISSION_GRANTED &&
                ThirdPermissionResult == PackageManager.PERMISSION_GRANTED &&
                ForthPermissionResult == PackageManager.PERMISSION_GRANTED ;
    }

【问题讨论】:

    标签: android gps android-permissions android-fusedlocation


    【解决方案1】:

    您需要像这样接收 GPS 状态的广播:

    public class GpsLocationReceiver extends BroadcastReceiver {        
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
                // here you can get the location or start a service to get it.
                // example 
                // Intent getLocationService = new Intent(context,YourService.class);
                // context.startService(getLocationService);
            }
        }
    }
    

    更新

    根据下面的评论 在你的代码中使用它

    manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if(manager.isProviderEnabled( LocationManager.GPS_PROVIDER)){
        // it is enabled
    }else{
        // it is not
    }
    

    【讨论】:

    • 我实际上只需要在获取位置之前检查是否启用了 gps。您能否详细说明如何在给定的场景中实现它?
    【解决方案2】:

    使用我的代码,如果 GPS 关闭,它会要求用户打开它。在 GPS 打开之前,它永远不会返回任何东西。

    注意:在将其用于棉花糖及以上手机之前,您必须获得位置许可。

    试试这个:

    @SuppressWarnings("MissingPermission")
    public class LocationFinder implements LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
        public static final String TAG = "LocationFinder";
        private static final String BROADCAST_GPS_REQ = "LocationFinder.GPS_REQ";
        private static final String KEY_GPS_REQ = "key.gps.req";
        private static final int GPS_REQUEST = 2301;
        private Activity activity;
        private FinderType finderType;
        private GoogleApiClient googleApiClient;
        private LocationRequest locationRequest;
        private long updateInterval;
        private long fastestInterval;
        private GpsRequestListener gpsRequestListener;
        private LocationUpdateListener locationUpdateListener;
    
        public LocationFinder(Activity activity, FinderType finderType) {
            this.activity = activity;
            this.finderType = finderType;
        }
    
        private void connectGoogleApiClient() {
            googleApiClient = new GoogleApiClient.Builder(activity)
                    .addApi(LocationServices.API).addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this).build();
            googleApiClient.connect();
        }
    
        private void createLocationRequest() {
            locationRequest = LocationRequest.create();
            locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            locationRequest.setInterval(updateInterval);
            locationRequest.setFastestInterval(fastestInterval);
        }
    
        private BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                int intExtra = intent.getIntExtra(KEY_GPS_REQ, 0);
                switch (intExtra) {
                    case Activity.RESULT_OK:
                        try {
                            LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, LocationFinder.this);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        gpsRequestListener.gpsTurnedOn();
                        break;
                    case Activity.RESULT_CANCELED:
                        gpsRequestListener.gpsNotTurnedOn();
                }
            }
        };
    
        public void gpsRequestCallback(GpsRequestListener gpsRequestListener) {
            this.gpsRequestListener = gpsRequestListener;
            LocalBroadcastManager.getInstance(activity).registerReceiver(receiver, new IntentFilter(BROADCAST_GPS_REQ));
        }
    
        public void config(long updateInterval, long fastestInterval) {
            this.updateInterval = updateInterval;
            this.fastestInterval = fastestInterval;
        }
    
        public void find(LocationUpdateListener listener) {
            this.locationUpdateListener = listener;
            createLocationRequest();
            connectGoogleApiClient();
        }
    
        private void find() {
            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(@NonNull LocationSettingsResult locationSettingsResult) {
                    Status status = locationSettingsResult.getStatus();
                    switch (status.getStatusCode()) {
                        case LocationSettingsStatusCodes.SUCCESS:
                            LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, LocationFinder.this);
                            break;
                        case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                            try {
                                status.startResolutionForResult(activity, GPS_REQUEST);
                            } catch (IntentSender.SendIntentException e) {
                                e.printStackTrace();
                            }
                            break;
                        case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                            Log.d(TAG, "No GPS Hardware");
                            break;
                    }
                }
            });
        }
    
        public void stopFinder() {
            if (googleApiClient.isConnected()) {
                LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
                googleApiClient.disconnect();
            }
            try {
                activity.unregisterReceiver(receiver);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        @Override
        public void onConnected(@Nullable Bundle bundle) {
            Log.d(TAG, "GoogleApiClient: Connected");
            find();
        }
    
        @Override
        public void onConnectionSuspended(int i) {
            Log.d(TAG, "GoogleApiClient: onConnectionSuspended");
        }
    
        @Override
        public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
            Log.d(TAG, "GoogleApiClient: onConnectionFailed");
        }
    
        @Override
        public void onLocationChanged(Location location) {
            if (finderType != FinderType.TRACK)
                stopFinder();
            locationUpdateListener.onLocationUpdate(new LatLng(location.getLatitude(), location.getLongitude()));
        }
    
        public void setGpsRequestListener(GpsRequestListener gpsRequestListener) {
            this.gpsRequestListener = gpsRequestListener;
        }
    
        public static void onRequestResult(Activity activity, int requestCode, int resultCode) {
            if (requestCode == GPS_REQUEST) {
                Intent intent = new Intent(BROADCAST_GPS_REQ);
                intent.putExtra(KEY_GPS_REQ, resultCode);
                LocalBroadcastManager.getInstance(activity).sendBroadcast(intent);
            }
        }
    
        public enum FinderType {
            // It will update the current GPS location once.
            GPS,
            //It will update the user location continuously.
            TRACK
        }
    
        public interface LocationUpdateListener {
            void onLocationUpdate(LatLng latLng);
        }
    
        public interface GpsRequestListener {
            void gpsTurnedOn();
    
            void gpsNotTurnedOn();
        }
    
    }
    

    要获取位置更新,请像这样初始化类:

    public class MyActivity extends AppCompatActivity implements
            LocationFinder.GpsRequestListener,
            LocationFinder.LocationUpdateListener {
    
        private LocationFinder locationUpdater;
            @Override
            public void onCreate(@Nullable Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                locationUpdater = new LocationFinder(this, LocationFinder.FinderType.TRACK);
                driverLocationUpdater.gpsRequestCallback(this);
                driverLocationUpdater.config(5000, 5000);
                driverLocationUpdater.find(this);
        }
    
        @Override
        protected void onDestroy(){
          super.onDestroy()
          driverLocationUpdater.stopFinder();
        }
    
        @Override
        public void onLocationUpdate(LatLng latLng) {
    
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        LocationFinder.onRequestResult(this, requestCode, resultCode);
        }
    
    }
    

    【讨论】:

    • 谢谢,让我试试这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    相关资源
    最近更新 更多