2

I am trying to get current location using FusedLocationAPI in my application. It works just fine in both android API>23 and <23. The issue is in case of GPS is not enabled, the app prompts the user to enable the GPS and after that the location returns null. But while restarting the app, it works just fine. In case of GPS not being turned OFF, the app just works fine. Is there any way that the app can wait for the GPS to be enabled before getting the location? below I am posting my code. Please have a look.

Inside 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");

Other methods:

       @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 ;
    }
Animesh Jena
  • 1,541
  • 1
  • 25
  • 44

2 Answers2

2

You need to recieve the broadcast of the GPS status something like this:

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

update

according to comment below use this in your code

manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if(manager.isProviderEnabled( LocationManager.GPS_PROVIDER)){
    // it is enabled
}else{
    // it is not
}
Yamen Nassif
  • 2,416
  • 2
  • 24
  • 48
  • I actually just need to check if the gps is enabled or not just before getting the location. Can you you please elaborate how to implement it in the given scenario? – Animesh Jena Sep 22 '17 at 10:12
1

Use my code, it will ask the user to turn on GPS if it is turned off. It will never return you anything until GPS turned on.

Note: you have to get location permission before using it for marshmallow and above mobiles.

Try this:

@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();
    }

}

To get location update initialize the class like this:

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

}
Bhuvanesh BS
  • 13,474
  • 12
  • 40
  • 66