in out app we have a module where you can send time records with GPS location.
In 40% of cases we have location problem because the module provides a location which is not real (sometime about 1 km away).
When we have this cases we open Google Maps app and it give us perfect location, then again out app and not-real location.
For getting the location we use this:
Slider s1 = new Slider();
Dialog dlg = makeDialog("HIGH PRECISION LOCATION...", s1, null, 'a');
dlg.addShowListener((ActionListener) e -> {
LocationManager locationManager = LocationManager.getLocationManager();
locationManager.setLocationListener(this, new LocationRequest(LocationRequest.PRIORITY_HIGH_ACCUARCY, 500));
loc = locationManager.getCurrentLocationSync(20000);
dlg.dispose();
if(loc == null) {
Slider s2 = new Slider();
Dialog dlg2 = makeDialog("GETTING LAST KNOWN LOCATION...", s2, "OK", 'a');
dlg2.addShowListener((ActionListener) e2 -> {
loc = locationManager.getLastKnownLocation();
if(loc == null) {
// location not found
Dialog.show("Attenzione!", "Posizione non trovata. E' consigliato di spostarsi all'aperto. "
+ "Tuttavia รจ possibile inviare la timbratura anche senza coordinate.", "Ok", null);
} else {
paintLocation(loc, GPSStateOn);
}
});
dlg2.show();
} else {
paintLocation(loc, GPSStateOn);
}
});
dlg.show();
-
#Override
public void locationUpdated(final Location location) {
switch (LocationManager.getLocationManager().getStatus()) {
case LocationManager.AVAILABLE:
GPSStateOn = true;
break;
case LocationManager.OUT_OF_SERVICE:
GPSStateOn = false;
break;
case LocationManager.TEMPORARILY_UNAVAILABLE:
GPSStateOn = false;
break;
}
if(loc != null) {
paintLocation(loc,GPSStateOn);
System.out.println("-----LATITUDINE: " + loc.getLatitude());
System.out.println("-----LONGITUDINE: " + loc.getLongitude());
}
}
We also have added the buil hint android.playService.location = true
ADDED 25/04/2020 11:58
I forgot to say that about 40% of cases it arrives at "GETTING LAST KNOWN LOCATION..." and then give perfect real location.
Android build hints:
last one is cutted... value is:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/><uses-feature android:glEsVersion="0x00020000" android:required="true"/><uses-permission android:name="android.permission.CAMERA"/><uses-feature android:name="android.hardware.camera" android:required="false"/>
ADDED 06/05/2020 10:48
Maybe this can be useful:
I noticed when the location takes a lot the GPS icon isn't displayed in the topbar
And when it takes less time the GPS icon is visible
Looking again at the code I noticed you used set location listener and getCurrentLocationSync which are mutually exclusive in this case. You need to pick one. If you give the former (which is the preferred way) enough time to work it should give you an accurate location. Notice the GPS position sometimes needs a couple of seconds for the initial reading.
Related
With the help of this question I was able to figure out how I can display a link inside a StyledText widget in SwT. The color is correct and even the cursor changes shape when hovering over the link.
So far so good, but the link is not actually clickable. Although the cursor changes its shape, nothing happens if clicking on the link. Therefore I am asking how I can make clicking the link to actually open it in the browser.
I thought of using a MouseListener, tracking the click-location back to the respective text the click has been performed on and then deciding whether to open the link or not. However that seems way too complicated given that there already is some routine going on for changing the cursor accordingly. I believe that there is some easy way to do this (and assuring that the clicking-behavior is actually consistent to when the cursor changes its shape).
Does anyone have any suggestions?
Here's an MWE demonstrating what I have done so far:
public static void main(String[] args) throws MalformedURLException {
final URL testURL = new URL("https://stackoverflow.com/questions/1494337/can-html-style-links-be-added-to-swt-styledtext");
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, true));
StyledText sTextWidget = new StyledText(shell, SWT.READ_ONLY);
final String firstPart = "Some text before ";
String msg = firstPart + testURL.toString() + " some text after";
sTextWidget.setText(msg);
sTextWidget.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
StyleRange linkStyleRange = new StyleRange(firstPart.length(), testURL.toString().length(), null, null);
linkStyleRange.underline = true;
linkStyleRange.underlineStyle = SWT.UNDERLINE_LINK;
linkStyleRange.data = testURL.toString();
sTextWidget.setStyleRange(linkStyleRange);
shell.open();
while(!shell.isDisposed()) {
display.readAndDispatch();
}
}
Okay I was being a little too fast on posting this question... There's a snippet that deals with exactly this problem and it shows, that one indeed has to use an extra MouseListener in order to get things working.
The snippet can be found here and this is the relevant part setting up the listener:
styledText.addListener(SWT.MouseDown, event -> {
// It is up to the application to determine when and how a link should be activated.
// In this snippet links are activated on mouse down when the control key is held down
if ((event.stateMask & SWT.MOD1) != 0) {
int offset = styledText.getOffsetAtLocation(new Point (event.x, event.y));
if (offset != -1) {
StyleRange style1 = null;
try {
style1 = styledText.getStyleRangeAtOffset(offset);
} catch (IllegalArgumentException e) {
// no character under event.x, event.y
}
if (style1 != null && style1.underline && style1.underlineStyle == SWT.UNDERLINE_LINK) {
System.out.println("Click on a Link");
}
}
}
});
I'm running an Android 5.1 project recently. And while LTE testing, I found a weird issue.
I want to get the Cid and Lac by using GsmCellLocation and PhoneStateListener
Here is my code of PhoneStateListener and GsmCellLocation:
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(new PhoneStateListener() {
#Override
public void onCellLocationChanged(CellLocation location) {
String txt = "";
if (location instanceof GsmCellLocation) {
if (location != null) {
//something process when location changed
GsmCellLocation l = (GsmCellLocation) location;
if (l_ != null) {
txt += "\ngetCellLocation: " + l_.getCid();
}
else {
txt += "\ngetCellLocation, location is null";
}
}
}
}
}, PhoneStateListener.LISTEN_CELL_LOCATION);
GsmCellLocation l_ = (GsmCellLocation) mTm.getCellLocation();
String txt = "";
if (l_ != null) {
txt = "getCellLocation: " + l_.getCid();
}
else {
txt = "getCellLocation, location is null";
}
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
I try to get the location via LTE by using PhoneStateListener in my code.
But It was not working as expected. However, 4G mobile data is working fine.
Only using GsmCellLocation is working fine. I can get the Cid and Lac from GsmCellLocation class.
After tracing the source code, I found that onCellLocationChanged of PhoneStateListener and getCellLocation of GsmCellLocation are running the same function named newFromBundle.
When running getCellLocation I saw this function is working fine with GsmCellLocation(bundle) of newFromBundle so that I can get the proper Cid and Lac by using GsmCellLocation class.
But onCellLocationChanged of PhoneStateListener is always return null with newFromBundle.
public static CellLocation newFromBundle(Bundle bundle) {
// TelephonyManager.getDefault().getCurrentPhoneType() handles the case when
// ITelephony interface is not up yet.
//switch(TelephonyManager.getDefault().getCurrentPhoneType()) {
switch(bundle.getInt("type", PhoneConstants.PHONE_TYPE_NONE)) {
case PhoneConstants.PHONE_TYPE_CDMA:
Log.e(TAG, "create CdmaCellLocation");
return new CdmaCellLocation(bundle);
case PhoneConstants.PHONE_TYPE_GSM:
Log.e(TAG, "create GsmCellLocation");
return new GsmCellLocation(bundle);
default:
return null;
}
}
It doesn't make sense. Why do I say that. I got three SIMs of South Korea, KT, LG U+ and SK Telecom.
These three SIMs are working fine with getCellLocation of GsmCellLocation.
KT and SK Telecom are working fine with onCellLocationChanged of PhoneStateListener.
Only LG U+ is malfunction by using onCellLocationChanged of PhoneStateListener.
I don't think my code is wrong. However, the vendor of the unit said I have to setup proper SubId to let PhoneStateListener initialized.
I'm a little confused about it. I don't have to set any SubId when using SK and SK Telecom, but why do Lg U+ need to do that? And after I set the SubId of LG U+, it still doesn't work.
Is there anyone meet the issue? This issue has already perplexed me for a long time.
How to get the most accurate geo-location on an android device, I tried following code snippet But on Nexus 4 and 10, it doesn't work even after the gps is turned on :
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
String locationProvider = locationManager.getBestProvider(new Criteria(), true);
if (locationProvider != null) {
//Log.d(TAG, "locationProvider is not null");
Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
if (lastKnownLocation != null) {
AppConstants.lat = latLon[0] = lastKnownLocation.getLatitude();
AppConstants.lon = latLon[1] = lastKnownLocation.getLongitude();
retryGenerating = false;
//Log.d(TAG, "lastKnownLocation, lat = " + AppConstants.lat + ", lon = " + AppConstants.lon);
}
}
The most accurate location you get by explicitly setting the LocationProvider to GPS Provider. Expect usually 3-6m accuracy, up to 30m in urban canyons.
You need to to be outside and have free view to sky.
Expect a delay of 20-40 s for the first valid location after going outside.
I have code that is used to show a device's location. It works just fine on the emulator and it takes me to the fake location at Microsoft. But it didn't work when I build it into the phone, it showed me the world map. Is this a known bug or I have done something wrong? Here is my code:
private GeoCoordinateWatcher loc = null;
private void button1_Click(object sender, RoutedEventArgs e)
{
if (loc == null)
{
loc = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
loc.StatusChanged += loc_StatusChanged;
}
if (loc.Status == GeoPositionStatus.Disabled)
{
loc.StatusChanged -= loc_StatusChanged;
MessageBox.Show("Location services must be enabled on your phone.");
return;
}
loc.Start();
}
void loc_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
if (e.Status == GeoPositionStatus.Ready)
{
Pushpin p = new Pushpin();
p.Template = this.Resources["pinMyLoc"] as ControlTemplate;
p.Location = loc.Position.Location;
mapControl.Items.Add(p);
map1.SetView(loc.Position.Location, 17.0);
loc.Stop();
}
}
}
Instead of using the StatusChanged event, you should use the GeoCoordinateWatcher.PositionChanged event, in from which you should use the GeoPositionChangedEventArgs.Position property, to reflect the changed location.
This is due to my location doesn't support by Bing Map. I couldn't use the Bing Map app installed in my phone neither. Hmm...
I am working on gps based application. I am using LocationProvider and calling setlocationListener through it code is like
LocationProvider lp = LocationProvider.getInstance(null);
if (lp != null) {
lp.setLocationListener(new LocationListenerImpl(), 2, 1, 1);
} else {
Dialog.alert("GPS NOT SUPPORTED!");
retval = false;
}
} catch (LocationException e) {
System.out.println("GPS Error: " + e.toString());
}
return retval;
}
private class LocationListenerImpl implements LocationListener {
public void locationUpdated(LocationProvider provider, Location location) {
if (location.isValid()) {
heading = location.getCourse();
longitude = location.getQualifiedCoordinates().getLongitude();
latitude = location.getQualifiedCoordinates().getLatitude();
altitude = location.getQualifiedCoordinates().getAltitude();
speed = location.getSpeed();
System.out.println("Current latitude:"+latitude);
System.out.println("Current longitude:"+longitude);
System.out.println("Current speed:"+speed);
// This is to get the Number of Satellites
String NMEA_MIME = "application/X-jsr179-location-nmea";
satCountStr = location.getExtraInfo("satellites");
if (satCountStr == null) {
satCountStr = location.getExtraInfo(NMEA_MIME);
}
// this is to get the accuracy of the GPS Cords
QualifiedCoordinates qc = location.getQualifiedCoordinates();
accuracy = qc.getHorizontalAccuracy();
}
}
it doesnt give an error but dont even work out so help with the same.the control dont get transferred to LocationListenerImpl()...
I am using BlackBerry_JDE_PluginFull_1.0.0.67 with eclipse-java-galileo-SR1-win32 on Blackberry 8800 simulator..
Any assistence is grately appreciated....
Thanking you in advance.
try this:
lp.setLocationListener(new LocationListenerImpl(), 2, -1, -1);
Acc to me
u set 2,1,1 which means that time interval is 2 sec after which location updation will be automatically called
1 sec indicates time out
1 sec indicates max age
ur gps times out before going ahead for gps location update method.so try it with setting it to default value = -1
You have to enable the GPS in the simulator, is it enabled when you try?
If you're getting a NullPointerException then you should check in the OS Options that the GPS has Location activated.