Advancing Backwards

Boldly going in the wrong direction since 2008

Browsing Posts tagged Mobile Programming

Here’s something handy I learned with Android the other day.

Lets say you have two activities. The first one, A, has a button that opens up the second activity, B. B, in turn has a button that returns the user to activity A. So, if you were to push the button in activity A, you would end up in activity B and vice versa. Pretty straight forward.

Now, lets say you just pushed the button in activity A, and ended up in activity B. You now want to go back to activity A. Pretty simple, just push the button in B, right? While if we push the button, we do in fact end up looking at activity A again, but it is not, in fact, the same instance of activity A that brought us to B in the first place.

continue reading…

So, lets say your application needs to let the user know when he/she is within, say, 1 mile of a given location. Or, you want to sort a series of locations based on how close to the user the points are in a straight line. How do you go about doing that? One way would be to use the Haversine formula to figure out the distances between two points, considering that Earth is spherical, but that takes math, and who likes doing that?

Thankfully, there is a much simpler way to figure out distances between two points on a map: the distanceTo() method in the Location class. Using this handy little method, we can quickly find out the distance between two locations:

double distance;

Location locationA = new Location("point A");

locationA.setLatitude(latA);
locationA.setLongitude(lngA);

Location locationB = new Location("point B");

locationB.setLatitude(latB);
LocationB.setLongitude(lngB);

distance = locationA.distanceTo(locationB);

One thing to note is that the distanceTo() method returns the distance in meters, so you will need to do the appropriate calculations if you want your distances in other units.

The application I’m currently working requires that the user has the GPS active on his/her phone. This can be either the coarse (cell tower triangulation) or fine (GPS satellites) methods, but whichever it is, the application needs the Geodata to function. This brought up the issue of, how do we know if the user actually has the GPS enabled on the phone? If they don’t, the application will still work, but all the coordinates it stores will end up being 0 degrees for both latitude and longitude, not very useful. To combat this, we can do a simple check when the application starts up to make sure the user knows that their GPS is disabled, and their location will be recorded as somewhere along the equator.

To start with, we need a locationManager object:

LocationManager locManager = (LocationManager) getSystemService(LOCATION_SERVICE);

Now that we have a location manager, its a simple process to check to see if the GPS is enabled or not:

if (!locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
      createGpsDisabledAlert();
}

This just checks to see if GPS is on, and if not, it calls a method, createGpsDisabledAlert(), which will build an alert dialog to warn the user that their GPS is off:

private void createGpsDisabledAlert(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your GPS is disabled! Would you like to enable it?")
     .setCancelable(false)
     .setPositiveButton("Enable GPS",
          new DialogInterface.OnClickListener(){
          public void onClick(DialogInterface dialog, int id){
               showGpsOptions();
          }
     });
     builder.setNegativeButton("Do nothing",
          new DialogInterface.OnClickListener(){
          public void onClick(DialogInterface dialog, int id){
               dialog.cancel();
          }
     });
AlertDialog alert = builder.create();
alert.show();
}

If the user decides they want to turn their GPS on, and they select the positive action of the dialog, showGpsOptions() is called. All this method does is to show the “Locations and Security” page from the Android settings menu. As a small sidenote, as of Android 1.5, there is no way to directly toggle the location settings through code, so we have to assume the user can figure it out if shown the proper screen. The code for showGpsOptions() is very simple:

private void showGpsOptions(){
		Intent gpsOptionsIntent = new Intent(
				android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
		startActivity(gpsOptionsIntent);
	}

And thats it! When the user starts the activity, if the GPS is off, the dialog will pop up asking them what they want to do. If the GPS is already on, nothing will happen. Sweet. The full code looks like the following:

protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     LocationManager locManager = (LocationManager) getSystemService(LOCATION_SERVICE);

     if (!locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
          createGpsDisabledAlert();
     }
}

private void createGpsDisabledAlert(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your GPS is disabled! Would you like to enable it?")
     .setCancelable(false)
     .setPositiveButton("Enable GPS",
          new DialogInterface.OnClickListener(){
          public void onClick(DialogInterface dialog, int id){
               showGpsOptions();
          }
     });
     builder.setNegativeButton("Do nothing",
          new DialogInterface.OnClickListener(){
          public void onClick(DialogInterface dialog, int id){
               dialog.cancel();
          }
     });
AlertDialog alert = builder.create();
alert.show();
}

private void showGpsOptions(){
		Intent gpsOptionsIntent = new Intent(
				android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
		startActivity(gpsOptionsIntent);
}