1
votes

Old question: Codename One app not provide real location

We still have problem getting current location.

Sometimes it's ok, "Localizzazione..." dialog shows, then location ok callback dispose the dialog.

Sometimes the dialog is never disposed and I don't see GPS in the top bar, which is visible when location is ok and dispose the dialog.

Slider s1 = new Slider();

Display.getInstance().callSerially(() -> {
    blocco_loc_in_corso = makeDialog("Localizzazione...", s1, null, 'a');
    blocco_loc_in_corso.show();
});

LocationManager locationManager = LocationManager.getLocationManager();
locationManager.setLocationListener(new LocationListener() {
    @Override
    public void locationUpdated(Location location) {
        if(location != null) {
            Display.getInstance().callSerially(() -> {
                if(blocco_loc_in_corso != null) {
                    blocco_loc_in_corso.dispose();
                }
            });
            paintLocation(location, true);
        }
    }

    @Override
    public void providerStateChanged(int newState) {

    }
}, new LocationRequest(LocationRequest.PRIORITY_HIGH_ACCUARCY, 1000));

I have this problem for at least 6 months. We only need to block user until we have his GPS location which may can change (GPS updates callback).

Edited:

public Dialog makeDialog(String label, Component c, String buttonText, char btIcon) {
    Dialog dlg_r = new Dialog();
    Style dlgStyle = dlg_r.getDialogStyle();
    dlgStyle.setBorder(Border.createEmpty());
    dlgStyle.setBgTransparency(255);
    dlgStyle.setBgColor(0xffffff);

    Label title = dlg_r.getTitleComponent();

    title.getUnselectedStyle().setFgColor(0xff);
    title.getUnselectedStyle().setAlignment(Component.LEFT);

    dlg_r.setLayout(BoxLayout.y());

    Label blueLabel = new Label(label);
    blueLabel.setShowEvenIfBlank(true);
    blueLabel.getUnselectedStyle().setBgColor(0xff);
    blueLabel.getStyle().setFgColor(0x0a0afc);
    blueLabel.getStyle().setAlignment(Component.CENTER);
    blueLabel.getUnselectedStyle().setPadding(1, 1, 1, 1);
    blueLabel.getUnselectedStyle().setPaddingUnit(Style.UNIT_TYPE_PIXELS);
    dlg_r.add(blueLabel);
    dlg_r.add(c);
    if (buttonText != null) {
        Button dismiss = new Button(buttonText);
        dismiss.getAllStyles().setBorder(Border.createEmpty());
        dismiss.getAllStyles().setFgColor(0);
        dismiss.getAllStyles().set3DText(true, true);
        dismiss.setIcon(FontImage.createMaterial(btIcon, dismiss.getStyle()));
        dismiss.addActionListener(((evt) -> {
            dlg_r.dispose();
        }));
        dlg_r.add(dismiss);
    }
    return dlg_r;
}
1
I need to see the code of makeDialog generally it shouldn't show at all if locationUpdated is invoked. It would also be interesting to log in providerStateChanged just to make sure everything is in order. - Shai Almog
I have edited the answer with makeDialog code - loreeemartiii

1 Answers

0
votes

To make sure this code is threadsafe make the following change:

public void locationUpdated(Location location) {
    locationFound = true;
    // ...
}

Then in the make dialog method:

dlg_r.addShowListener(e -> {
     if(locationFound) {
         dlg_r.dispose();
     }
});

Since this event can happen in the dead time of showing the dialog transition.