Android GPS isn't providing updates anymore - android

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.

Related

LocationListener how to stop when onLocationChanged() is called?

I want locationlistener to stop using gps when the location is changed, is this possible ?
public class MyActivity extends AppCompatActivity implements LocationListener {
private LocationManager locationManager;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
#Override
public void onLocationChanged(Location location) {
locationManager.removeUpdates(this);
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
}
here this is the reference to LocationListener Interface.
If you want only single location update then you can call
locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, this, Looper.getMainLooper());

LocationListener not working, without any error in LogCat

im a bit desperate because somehow, my code just stopped working.
Code:
public class MainBoard extends FragmentActivity implements OnClickListener, OnTouchListener, LocationListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
//Location Listener
current_location = new MarkerOptions();
current_location.icon(BitmapDescriptorFactory.fromResource(R.drawable.location));
LocationManager locationManager = (LocationManager)getSystemService(getApplicationContext().LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, this);
}
//LocationListener
#Override
public void onLocationChanged(Location location) {
Log.d("LocationListener", "LocationChanged");
updateCurrentLocation(location);
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
#Override
public void onProviderEnabled(String provider) {
Log.d("LocationListener", "onProviderEnabled");
instantiatePosition();
}
#Override
public void onProviderDisabled(String provider) {
Log.d("LocationListener", "onProviderDisabled");
showGPSOfflineAlert();
}
}
I can't really see the error here, LogCat doesn't report any error, but no method is called, not even onProviderEnabled or onProviderDisabled...
Thanks in advance :)

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)

Android GPS onLocationChanged never called

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.

How to obtain the GPS position of the user and actualice a textview everytime the position has changed?

I'm trying to make a simple activity that reads the GPS position of the user and actualices a simple textview everytime the position of the user changes.
I find some examples en google but all of them are not nice examples, because only capture the position of the user ONE time, and i need that the textview get's actualiced everytime the position changes with the new latitude and longitude of the user.
I tryed to do a thread but it fails and i think it is not necesary to do a thread, im in the wrong way.
Code examples are welcome
EDIT: i'm adding the solution proposed by the user NickT. This solution fails. I dont know why but only actualizes two times the textview, with the two first GPS values that i pass to the emulator with DDMS.... after this the thextview isn't getting actualiced more times... ¿why?. I make a breakpoint in onLocationChanged, and it only get's called the first two times i send a gps positiones... but never more. ¿what is happening?
public class GpsMiniActivity extends Activity implements LocationListener{
private LocationManager mLocMgr;
private TextView tv1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout rl = new FrameLayout(this.getApplicationContext());
LinearLayout ll= new LinearLayout(this.getApplicationContext());
ll.setOrientation(LinearLayout.VERTICAL);
setContentView(rl);
rl.addView(ll);
tv1=new TextView(getApplicationContext());
ll.addView(tv1);
//setContentView(R.layout.main);
mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,
500, 0, this);
}
#Override
public void onLocationChanged(Location location) {
tv1.setText("Lat " + location.getLatitude() + " Long " + location.getLongitude());
}
#Override
public void onProviderDisabled(String provider) {}
#Override
public void onProviderEnabled(String provider) {}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
Absolute minimum example:
public class GpsMiniActivity extends Activity implements LocationListener{
private LocationManager mLocMgr;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,
500, 0, this);
}
#Override
public void onLocationChanged(Location location) {
TextView tv = (TextView) findViewById(R.id.tv1);
tv.setText("Lat " + location.getLatitude() + " Long " + location.getLongitude());
}
#Override
public void onProviderDisabled(String provider) {}
#Override
public void onProviderEnabled(String provider) {}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
You'll also need permission ACCESS_FINE_LOCATION in your manifest and a Textview id tv1 in main.xml

Categories

Resources