0

I'm developing an app and using following code to fetch current coordinates of the user (from this library):

   ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(getBaseContext());
          locationProvider.getLastKnownLocation().subscribe(new Action1<Location>() {
                  @Override
                  public void call(Location location) {
                  currentLatDouble = location.getLatitude();
                  currentLngDouble = location.getLongitude();
                  Toast.makeText(getBaseContext(), "User's location retrieved using library for android M in btnRetryGpsEvents()", Toast.LENGTH_SHORT).show();
                }
          });

The problem is that this code sometimes don't even gets executed even after all the conditions are true. See the code below:

    if (currentLatDouble != null || currentLngDouble != null) {
            retrieveHWrapper();
            progressBarLoadingRequests.setVisibility(View.VISIBLE);
            btnRetryGps.setVisibility(View.INVISIBLE);
            gps_off.setVisibility(View.INVISIBLE);
            Toast.makeText(getBaseContext(), "Loading hrequests...", Toast.LENGTH_LONG).show();
         } else {
            gps_off.setVisibility(View.VISIBLE);
            progressBarLoadingRequests.setVisibility(View.INVISIBLE);
            btnRetryGps.setVisibility(View.VISIBLE);
            btnRetryGps.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                btnRetryGpsEvents();
                int hasWriteContactsPermission = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) & ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) & ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) & ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_SETTINGS);
                                    if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
                                        if (ActivityCompat.checkSelfPermission(getBaseContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getBaseContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getBaseContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getBaseContext(), Manifest.permission.WRITE_SETTINGS) != PackageManager.PERMISSION_GRANTED) {
                                            // TODO: Consider calling
                                            //    ActivityCompat#requestPermissions
                                            // here to request the missing permissions, and then overriding
                                            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                            //                                          int[] grantResults)
                                            // to handle the case where the user grants the permission. See the documentation
                                            // for ActivityCompat#requestPermissions for more details.
                                            if (!ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) && !ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) && !ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) && !ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_SETTINGS)) {
                                                showMessageOKCancel("You need to allow access to few permissions so that the app can work as expected.",
                                                        new DialogInterface.OnClickListener() {
                                                            @Override
                                                            public void onClick(DialogInterface dialog, int which) {
                                                                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.WRITE_SETTINGS},
                                                                        REQUEST_RUNTIME_PERMISSION);
                                                            }
                                                        });
                                                return;
                                            }
                                            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.WRITE_SETTINGS},
                                                    REQUEST_RUNTIME_PERMISSION);
                                            return;
                                        }
                                    }

                                    ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(getBaseContext());
                                    locationProvider.getLastKnownLocation()
                                            .subscribe(new Action1<Location>() {
                                                @Override
                                                public void call(Location location) {
                                                    currentLatDouble = location.getLatitude();
                                                    currentLngDouble = location.getLongitude();
                                                    Toast.makeText(getBaseContext(), "User's location retrieved using library for android M in btnRetryGpsEvents()", Toast.LENGTH_SHORT).show();
                                                }
                                            });
                                    Toast.makeText(getBaseContext(), "called from btnRetryGpsEvents() - GPS available", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }

After running the above code, the toast due to this code is shown: Toast.makeText(getBaseContext(), "called from btnRetryGpsEvents() - GPS available", Toast.LENGTH_SHORT).show(); which means that this is getting executed, but the toast just above it viz.: Toast.makeText(getBaseContext(), "User's location retrieved using library for android M in btnRetryGpsEvents()", Toast.LENGTH_SHORT).show(); is not shown which means it doesn't gets executed!

How this can happen? What's going wrong here????

Please let me know.

P.S.: This problem is occuring only in Android M and above.

Hammad Nasir
  • 2,889
  • 7
  • 52
  • 133
  • The toast just above it is inside an interface method. I believe it would be called only when the callback arrives – Shaishav Aug 11 '16 at 11:30
  • @Shaishav what do you mean exactly? Does this code taking time in returning location? What should I do to ensure that some latitude and longitude must be retrieved? – Hammad Nasir Aug 11 '16 at 11:32
  • Yeah...I'm not even aware of that interface so I cannot comment how can you make it fast. But, note that it'll only be called when the callback arrives. If you want to show it in any case, put it outside the interface method (like your other toast) – Shaishav Aug 11 '16 at 11:36
  • haha.. my main aim is not to show the `Toast`. It is to fetch the coordinates. Is there any better way to do so? – Hammad Nasir Aug 11 '16 at 11:37
  • That i believe will take some time. I'm not aware of your use case, but I have a feeling that if you move your other toast inside the method too, they will appear one after another only when legitimate data comes (assuming that is what your users want) – Shaishav Aug 11 '16 at 11:51
  • @Shaishav bro, what I want is I want to retrieve the current coordinates of the user. I am going to hide these `Toasts`, they are there for testing purpose only. I hope you got my point. Main aim is not to show the `Toasts`, it is to retrieve user's current coordinates. – Hammad Nasir Aug 11 '16 at 12:08
  • Then I guess you need to wait till the data arrives! – Shaishav Aug 11 '16 at 12:10
  • Yeah! Thank god you got my point. So, now please tell me a better and faster way to retrieve user's current coordinates. – Hammad Nasir Aug 11 '16 at 12:11
  • THAT is a completely different question from your original one. You should ask another question since it'll not really help the people visiting this page based on the title. – Shaishav Aug 11 '16 at 12:13

1 Answers1

0

Please check code inside ReactiveLocationProvider. I think it implements LocationListener. Guessing that getLastLocation called inside. It will give location only if last location available. Or please provide code so we can check.

Ramit
  • 416
  • 3
  • 8
  • this is a library I'm using: https://github.com/mcharmas/Android-ReactiveLocation – Hammad Nasir Aug 11 '16 at 12:09
  • I found it working fine on M device. It might be possible that particular device on which you are testing have some problem or dont have last known location. It is possible that you can not get location immediately as it is designed in such way. So change your design and handle such cases. – Ramit Aug 11 '16 at 13:11