Discussing the nuts and bolts of software development

Friday, July 17, 2009

 

Here I am!

This application allows you to send URL link with your location through SMS message. Receiver will be able to see your location on the map(Google Maps) by simply clicking on the link. First thing we need to do is to get our location using GPS. Android SDK has set of classes and interfaces which makes dealing with location services quite easy. Another positive thing is that SDK is well documented. We need to get an instance of LocationManager:
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Now that we have location manager we need to request location updates from it. The location is requested using requestLocationUpdates method, it registers the current activity to be notified periodically by the named provider. In our case provider is GPS. For tracking location changes we are using LocationListener which is for receiving notifications from the LocationManager when the location has changed.

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, 
                new LocationListener() {
                    public void onLocationChanged(Location location) 
                    {
                        double lat = location.getLatitude();
                        double lng = location.getLongitude();                        

                        latitude.setText( Double.toString( lat ) );
                        longitude.setText( Double.toString( lng ) );
                    }

                    public void onProviderDisabled(String provider){}
                    public void onProviderEnabled(String provider){}
                    public void onStatusChanged(String provider, int status,Bundle extras){}
        });

Just getting last known location...
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Good! We got the location. Now we just need to generate the URL and send SMS. Android offers full access to SMS functionality from within your applications with the SMSManager. We are geeting a reference to the SMS Manager using the static method SmsManger.getDefault. For sending SMS message use sendTextMessage, which allows you to send SMS message by specifying text message and receiver's phone number.
final SmsManager sm = SmsManager.getDefault();
String phoneNumber = number.getText().toString();
String URL = "http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=";
URL += latitude.getText() + "," + longitude.getText(); 
sm.sendTextMessage(phoneNumber, null, URL, null, null);
That's it! Just one note - in order to send SMS message and request GPS location application require following two permissions:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.SEND_SMS"/> 

Thanks to my friend Aaron Olson from Macadamian for the help in testing this code.

Source

Labels: , ,


This page is powered by Blogger. Isn't yours?