Android GPS location updates doesn't work - android

I am trying to get the GPS location of my HTC magic using the following code :
public class TestGPS extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView tv1 = (TextView)findViewById(R.id.tv1);
final TextView tv2 = (TextView)findViewById(R.id.tv2);
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener()
{
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
public void onProviderEnabled(String provider)
{
}
public void onProviderDisabled(String provider)
{
}
public void onLocationChanged(Location location)
{
tv1.setText(String.valueOf(location.getLatitude()));
tv2.setText(String.valueOf(location.getLongitude()));
}
});
}
}
I waited for 5 minutes but none of the listener methods were called. The GPS icon shows up, stays there for some time but for some reason I don't get a fix, even outdoors in bright sunlight. I am testing on a HTC Magic with 1.6 SDK.
Can someone please tell me what's wrong with the code? Thanks.

The code looks fine. Make sure you have the ACCESS_FINE_LOCATION permission. Make sure your GPS is not broken -- in other words, does the built-in Maps application know where you are? Make sure your application works on the emulator with a fix you supply via the emulator.
Here is a sample project that uses GPS and that works fine on hardware.

Related

Unable to get GPS location updates on Oreo

I have tried (but in vain) to force request GPS location updates on my Android Widget (Android Oreo).
The widget is set to update every 15-20 minutes and fetch the current location of the phone and display.
However, it only picks up the location of the phone where it was installed and subsequent runs don't return a new location even when the phone moves.
I read through the solution provided here How does it work - requestLocationUpdates() + LocationRequest/Listener and implemented it, but it doesn't seem to help.
LocationManager locationManager = (LocationManager)
context.getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
300000L, 500.0f, new LocationListener() {
#Override
public void onLocationChanged(android.location.Location location) {
if(location != null)
{
LocationPreferences.setLastLocationLatLon(context,location);
}
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
});
Having said that, it is more reliable when it's in one of the activities (and not in a widget), but given that the widget is supposed to perform background location updating, it doesn't work, the onlocationchanged never gets called (I'm sensing it's silently killed in the background)
Has anyone created/ worked with Android widgets using GPS on Oreo? Any help in the right direction will be appreciated. Thank you very much in advance.

Real time data update

My app uses google map to show user's location in real time. Therefore I would like it to be updated with every data change on my Parse server.
I've looked all over the net and as I see it I have 3 options:
using syncAdapter - the problem is that everywhere it is written that it does not meant for real time data transfer (I don't really understand why), on other hand it must be more efficient than updating every 5 sec.
using asyncTask - the problem is that it probably consumes a lot of battery and data usage to run it every 5 sec.
using service - same problem as asyncTask.
As i'm very confused, please help me understand what is the appropriate way to implement my real time map.
Thank's all
The best way i know is to use LocationListener to update the location only if it has been changed. You can use the onLocationChanged() method to update the location.
public class LocationHelper {
private LocationManager locationManager;
private Location currentLocation;
public LocationHelper(Context context) {
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
locationManager.removeUpdates(locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
public void setLocation(Location location) {
currentLocation = location;
}
private final LocationListener locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
setLocation(location);//Only update location if it has changed.
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
#Override
public void onProviderEnabled(String provider) {}
#Override
public void onProviderDisabled(String provider) {}
};
}
If the location hasn't been changed, you can just remember the last known location.

How to determine GPS getting unlocked while running the application in android?

I need to check whether GPS is locked in my application. I am able to check whether GPs is enabled in the phone. But once the GPS is locked, while running the application, it gets unlocked and I noticed that the GPS icon of the phone is blinking. I need to show an animation similar to that of phone in my application to acknowledge the user the GPS is unlocked. Anybody who knows the solution please post it.
Thanks in advance....
This is what I use. Start your animation when you requestLocationUpdates and end it when you get the onLocationChanged call. See my code below, it should solve your issue.
public class YourLocationClass implements LocationListener {
private LocationManager lm;
public void getLocation(Context ctx) {
lm = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 15000, 25, this);
//START YOUR ANIMATION
}
public void onLocationChanged(Location location) {
//HERE YOU STOP YOUR ANIMATION AND GET NEW LOCATION
}
public void onProviderDisabled(String provider) {
// DO SOME STUFF
}
public void onProviderEnabled(String provider) {
// DO SOME STUFF
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// DO SOME STUFF
}
}

Display the NMEA sentence?

Any one help to display the NMEA sentence like $GPRMC .....
I am using the below code:
public class SampleNMEA extends Activity implements LocationListener, GpsStatus.Listener,GpsStatus.NmeaListener{
LocationManager lm;
TextView output;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
output = (TextView)findViewById(R.id.out);
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000,10,this);
final Location location=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
output.append("\nBelow Data is about the your location:");
output.append("\n\n\nLatit:"+location.getLatitude());
output.append("\n\nLongitude:"+location.getLongitude());
output.append("\n\nTime: "+location.getTime());
}
public void onLocationChanged(Location location)
{
output.append("\n\n\nLatitude:"+location.getLatitude());
output.append("\n\nLongitude:"+location.getLongitude());
output.append("\n\nTime: "+location.getTime());
}
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2)
{
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000,10,this);
final Location location=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
output.append("\n\n\nLatitude:"+location.getLatitude());
output.append("\n\nLongitude:"+location.getLongitude());
output.append("\n\nTime: "+location.getTime());
}
public void onGpsStatusChanged(int event)
{
}
public void onNmeaReceived(long timestamp, String nmea)
{
output.append("NMEA Sentence: "+nmea);
}
}
Any one help I added permissions in manifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
Change your provider to GPS_PROVIDER.
You can get the nmea sentence by Gps provider.
Change this line
**lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000,10,this);**
to
**lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000,10,this);**
Now check in onNmeaReceived() method.
Just add a NmeaListener
public class SampleNMEA extends Activity implements LocationListener, GpsStatus.Listener,GpsStatus.NmeaListener{
LocationManager lm;
TextView output;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
output = (TextView)findViewById(R.id.out);
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000,10,this);
final Location location=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
output.append("\nBelow Data is about the your location:");
output.append("\n\n\nLatit:"+location.getLatitude());
output.append("\n\nLongitude:"+location.getLongitude());
output.append("\n\nTime: "+location.getTime());
//add listener here
lm.AddNmeaListener(this);
}
I have Samsung Galaxy Spica and yes, it doesn't work. However, I've installed 'ultra gps logger' (link) and NMEA data is correctly read. Perhaps author of this tool used different solution for reading NMEA which means that is possible.
Are you calling addNmeaListener(GpsStatus.NmeaListener) to set that class as a listener? If not, you should!
To do so, call it like lm.addNmeaListener(this);.
Good luck &
Regards
The conclusion being reached in this discussion here, is that NMEA doesn't work on some phones (the onNmeaReceived event never fires). ...but would love to be corrected on this.
If fakeGps is in list of installed apps, be sure to delete it and restart device in order to start receiving Nmea info. You can install free Nmea Info monitor from play market to test Nmea receiver on your device.

Getting an updated location in Android

I'm using the code shown below to get an updated value for location every time a button is clicked. When my activity is resumed I get an update every second, so that when I call getLastKnownLocation I expect to have a location that have been updated in the last second.
Is that the correct way to do that?
I would expect the onLocationChanged event to be triggered every time I execute a 'geo fix' command (or max after 1s since I request update every 1s), but it's only triggered the first time. Why?
Any help/suggestion welcome!
Thanks
package org.digitalfarm.atable;
...
public class Atable extends Activity {
private Button mSearchButton;
private TextView mytext;
private LocationManager locationManager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSearchButton = (Button)this.findViewById(R.id.button);
mytext = (TextView) findViewById(R.id.dude);
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
final Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
mSearchButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
}
});
}
//Start a location listener
LocationListener onLocationChange=new LocationListener() {
public void onLocationChanged(Location loc) {
//sets and displays the lat/long when a location is provided
String latlong = "Lat: " + loc.getLatitude() + " Long: " + loc.getLongitude();
mytext.setText(latlong);
}
public void onProviderDisabled(String provider) {
// required for interface, not used
}
public void onProviderEnabled(String provider) {
// required for interface, not used
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
// required for interface, not used
}
};
//pauses listener while app is inactive
#Override
public void onPause() {
super.onPause();
locationManager.removeUpdates(onLocationChange);
}
//reactivates listener when app is resumed
#Override
public void onResume() {
super.onResume();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,100.0f,onLocationChange);
}
}
The time, specified in requestLocationUpdates is the shortest possible time interval for which a maximum of one update will occur. So you have registered for at most one update every second, but the actual update may take more that that to occur, and, furthermore, no guarantee is given for that.
From the documentation:
minTime - the minimum time interval for notifications, in milliseconds. This field is only used as a hint to conserve power, and actual time between location updates may be greater or lesser than this value.
Try to set that time interval to 0 and if there are no other problems (although it all seems fine to me), you should start getting the updates "as frequently as possible".

Categories

Resources