Question

I want to check, if the device gets data from satellites. If I get my current location, then I do something. Otherwise, if I haven't got any coordinates yet, I want to Toast me a message. How can I know, whether I've already got the coordinates or not?

GeoPoint h = null;
double latitude = 10, longitude = 10;
public void onClick(View v) 
{
    switch (v.getId())
    {
      case R.id.get_GPS:
          final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
          if(isInternetOn() && manager.isProviderEnabled(LocationManager.GPS_PROVIDER))
          {
//I want here to check, if I get latitude and longitude values, which differ from 10 x 10, I go to my map, otherwise I say, that there's no information received from satellite.
/*
              h = new GeoPoint((int) (latitude * 1000000), (int) (longitude * 1000000));

              if(h.getLatitudeE6()==h.getLongitudeE6() && h.getLongitudeE6()==10000000)
              {
                  Toast.makeText(this, "No information received from satellite yet.", Toast.LENGTH_LONG).show();
                  break;
              }
              */
              Intent i = new Intent (this,getGPS.class);
              startActivity(i);
          }
          else
          {   
              if(!isInternetOn() && !manager.isProviderEnabled( LocationManager.GPS_PROVIDER))
              {
                  buildAlertMessageNoGpsNoInternet();
                  break;
              }

              if(!isInternetOn())
              {
                  buildAlertMessageNoInternet();
                  break;
              }
              if(!manager.isProviderEnabled( LocationManager.GPS_PROVIDER))
              {
                  buildAlertMessageNoGps();
                  break;
              }
          }

        break;
      case R.id.about_button:
        Intent j = new Intent(this, About.class);
        startActivity(j);
        break;
      case R.id.exit_button:
        this.moveTaskToBack(true);
        break;
    }
}

Sam, I get Null Pointer Exception in "if":

if(manager.isProviderEnabled(LocationManager.GPS_PROVIDER))
          {

              Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

              /*Here ->*/if(location.getTime() > ((Calendar.getInstance().getTimeInMillis()) - 10000))
              {
                  Intent i = new Intent (this, getGPS.class);
                  startActivity(i);
              }

              else 
              {
                    // Wait for new location
                    LocationListener locationListener = new myLocationListener();
                    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locationListener);
              }
}

Logcat

05-05 11:42:49.444: E/AndroidRuntime(300): java.lang.NullPointerException

Was it helpful?

Solution

I would try something along the lines of this:

public class Example extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        if(location.getTime() > Calendar.getInstance().getTimeInMillis() - 2 * 60 * 1000) {
            // Do something with the recent location fix
        }
        else {
            // Wait for new location
            LocationListener locationListener = new myLocationListener();
            manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locationListener);
        }
    }

    private class myLocationListener implements LocationListener {
        public void onLocationChanged(Location location) {
            if (location != null) {
                Log.v("Location Changed", location.getLatitude() + " " + location.getLongitude());
                // Do something with current location
            }
        }

        // Must implement missing functions!
    }
}

Addition

If location is null here that probably means that there is no last known location for whatever reason. Simple fix:

if(location != null && location.getTime() > ((Calendar.getInstance().getTimeInMillis()) - 10000))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top