Android GPS onLocationChanged never called - android

I have an android application and I would like to obtain the location.
For this I follow the android documentation, but it doesn't work. The onLocationChanged method is never called.
public class GPSTestActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
Toast.makeText(this, "GPS available", 3000).show();
}
else
{
Toast.makeText(this, "GPS not available", 3000).show();
}
LocationListener locationListener = new LocationListener()
{
public void onLocationChanged(Location location)
{
System.out.println("RECEIVED LOCATION");
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
public void onProviderEnabled(String provider)
{
}
public void onProviderDisabled(String provider)
{
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
In the AndroidManifest.mf I've include the permision:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
I'm trying to test the application with the emulator in DDMS console. I set a longitude and a latitude, then presss the send button, but the onLocationChanged method is not called.
What I'm doing wrong?
Thank in advance

put parms to the call:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10f, locationListener);

I've found the problem.
I was testing with Android 2.3.3. and there is a bug. Testing with Google API (Level 10) works perfect.

Related

Crush when turn off the gps

I receive crush, when user turn off the gps.
I have code like this to detect location`s user
if(locationManager == null) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000L,
15f, mLocationListener);
} else if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000L,
15f, mLocationListener);
} else {
Toast.makeText(this, "Please turn on gps on ypur phone", Toast.LENGTH_SHORT).show();
}
When user open app with gps, then turn off it, I receive crush like this
java.lang.AbstractMethodError: abstract method "void android.location.LocationListener.onProviderDisabled(java.lang.String)"
I tried to detect when user turn off gps, but I get crush earlier, then receiver detect
Find the declaration of mLocationListerner. Surely it overrite onLocationChanged(). But you may have forgotten override onProviderDisabled(). Do it even if it's empty. Override also onStatusChanged() and onProviderEnabled().
Luck.
LocationListener mLocationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
....
}
#Override
public void onProviderDisabled(String provider) { }
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
#Override
public void onProviderEnabled(String provider) { }
};

Android GPS isn't providing updates anymore

My own android app isn't providing me with anymore GPS location updates. I've tried to build the simplest app possible and it's still not working. There's a blinking GPS icon in the status bar (so the GPS can't be turned off correct?), however I don't get locationChanged updates.
I have absolutely no clue what the problem is.
Manifest includes this:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Mainactivity.java
public class MainActivity extends ActionBarActivity {
private LocationManager locationManager;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
textView.setText(location.getLatitude() + " " + location.getLongitude());
}
public void onStatusChanged(String provider, int status, Bundle extras) { }
public void onProviderEnabled(String provider) { }
public void onProviderDisabled(String provider) { }
};
}
Change Activity to also implement LocationListener.
public class MainActivity extends ActionBarActivity implements LocationListener{
take the functions out of inner class, change the locationListener to this.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0
, this); //note changed last parameter to this.
}
//The following methods were inside the inner class. They were not created in time
//for oncreate to requestLocationUpdates.
public void onLocationChanged(Location location) {
textView.setText(location.getLatitude() + " "
+ location.getLongitude());
}
public void onStatusChanged(String provider, int status, Bundle extras) { }
public void onProviderEnabled(String provider) { }
public void onProviderDisabled(String provider) { }
Try that and see if it works better. The Activity itself is now a LocationListener, and the updates will come to this class.

No respond when reading Android GPS

I want to read GPS data from Android. I have written some code follow online tutorial
public class MainActivity extends Activity
{
private DataCollection mDataCollection;
private LocationManager mLocationManager;
private Location mLocationGPS;
private String strLocationProvider = "";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Config.mContext = this;
mLocationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
mLocationGPS = getLocationProvider(mLocationManager);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
}
protected void onResume()
{
super.onResume();
// mDataCollection.register();
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
}
protected void onPause()
{
super.onPause();
// mDataCollection.unregister();
mLocationManager.removeUpdates(locationListener);
}
public void startCollection(View view)
{
// mDataCollection.register();
}
public void stopCollection(View view)
{
// mDataCollection.unregister();
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
if (location != null)
{
double longitude = location.getLongitude();
}
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
private Location getLocationProvider(LocationManager lm){
Location retLocation = null ;
Criteria mCriteria01 = new Criteria();
mCriteria01.setAccuracy(Criteria.ACCURACY_FINE);
mCriteria01.setAltitudeRequired(false);
mCriteria01.setBearingRequired(false);
mCriteria01.setCostAllowed(true);
mCriteria01.setPowerRequirement(Criteria.POWER_HIGH);
strLocationProvider = lm.getBestProvider(mCriteria01, true);
retLocation = lm.getLastKnownLocation(strLocationProvider);
return retLocation;
}
}
When I put a break point atdouble longitude = location.getLongitude();, it never stop at that break point. I also have include the permission in manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.collectdata"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Could someone help me. Thanks in advance
Are you moving at least 10 meters while testing this? That's what the "10" means in mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
Also, you don't need that line in both onCreate() and onResume(). Just remove it from your onCreate() method (though it shouldn't cause problems, just is neater).
http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(long, float, android.location.Criteria, android.app.PendingIntent)

How to observe Gps Provider continually?

I want to know when the GPS on the device is tuned on, and the location is changed.
Here is what I've done:
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
public class MyLocationListener implements LocationListener
{
#Override
public void onLocationChanged(Location loc)
{
Toast.makeText( getApplicationContext(),"onlocation changed",Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),"providerdisabled",Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),"provideenabled",Toast.LENGTH_SHORT).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
It is working fine when my app runs in the foreground, but when I close it, I won't get any more updates. I'd like to know how to get this information continuosly until my application is uninstalled.
You need to put this in a service and start the service sticky.
Also you should think about using the FusedLocationManager by using GooglePlayServices.
For more info about GooglePlayServices and locations read here

My GPS returns always 0 - 0 (HTC One X)

I try to get my location by this way:
public void onCreate(Bundle savedInstanceState) {
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 5, mlocListener);
}
public class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location loc) {
latitude = loc.getLatitude();
longitude = loc.getLongitude();
Toast.makeText(getApplicationContext(), "Location found", Toast.LENGTH_SHORT ).show();
}
#Override
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), "GPS disabled", Toast.LENGTH_SHORT ).show();
}
#Override
public void onProviderEnabled(String provider) {}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
Here are my permissions:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
When I try on my Eclipse emulator, this works perfectly, the Toast appears.
But on my HTC One X, it never appears...
Do you know why?
Thanks.

Categories

Resources