Friday, September 30, 2011

Exploiting Android GPS Capabilities

I still remember those times when the first smart phones came to be part of our everyday life. At first they were just simple devices used to communicate with people that were physically separated. That was fabulous, communicate with someone that was miles and miles away from you was just a dream. Time passed and those devices came to revolutionize the way we would use them. A built-in camera, microphones, games, SMS, Multimedia Messaging, Instant Messaging sounded like a good deal. I thought that was all we could come up with. Smart phones we part of our more intimate moments. Things kept changing as time had passed and mobile technology was not the exception. Now our mobile devices are capable of connecting to satellites out there in the space. How cool is that? Well, in reality Android phones can do much more than that; they can localize a user not only using the Global Positioning System but a combination of technologies such as GPS, mobile networks, and radio communication in order to locate a user. In this post, I will be giving the basics on GPS localization in our android devices. As a developer I have found this interesting since having the ability to know where a user is, there is a big variety of services that can benefit smart phone users. 

Let's get to our main task: learn how to use GPS features in Android.

Before we start, make sure you have the following already installed:

          1) Eclipse (freely available on-line)
          2) Android SDK (freely available on-line)

Now lets create a Android Project:


Once you have created the project, it is time to create our first Java class and add it to the project:


Now that you have created a Java class make sure you override the onCreate() method:


Make sure to import the following classes:

import android.location.Criteria;
import android.location.LocationListener;
import android.location.LocationManager;

These contain all that is needed to request user location from your Android device. Now it is time to instantiate some objects that will request for the user location:

As a matter of better programming practices, I have created the following protected variables in my class:

protected LocationListener locationListener;
protected LocationManager locationManager;

It is up to you if you follow this tip or you can directly implement those in the onCreate() method. They have to be instantiated:

locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener(getApplicationContext()); 

Note: locationListener is a class that I implemented in order to keep things organized. If you want to follow the same practice, make sure you create a class MyLocationListener that implement LocationListener like this:

public class MyLocationListener implements LocationListener
{
       //Methods that will be helpful to implement
      /*
     * Called when a new location is found by the network location provider. This overrides.           
     */
    public void onLocationChanged(Location location)
    {
          /*
           * Make use of location obtained by device 
           */
           if(location != null)
          {
          
               //display location in latitude and longitud
               text = "Lat: "+ location.getLatitude() 
               + "\nLon: " + location.getLongitude() + "\n\n";
  
           }
    }

    // You will have to override the 
    public void onStatusChanged(String provider, int status, Bundle extras)
    {
          /can be left blank
    }
    //and also
    public void onProviderEnabled(String provider)
    {
           //can be left blank
    }
    //as well as 
    public void onProviderDisabled(String provider)
    {
         Toast toast = Toast.makeText(context, provider + " is disabled!", Toast.LENGTH_LONG);
          toast.show();
    }
}

Note: the text variable that is being used in the onLocationChanged above is a CharSequence variable that can be displayed using an EditText object or any other way you want to display your gps data.

You can also implement the following code that is used to START the service, this code goes in your class that contains the onCreate() method. 

GPS_PROVIDER is used to get location just from GPS.


Or if you wish to get you location from the network you can use the following code:


Now that you have your locationManager running, you will need to stop the service at some point. This can be done anywhere you want but it has to be called once you do not want to continue collecting GPS data. The code looks like this:

This is pretty much what has to be done in order to obtain user location at any given time. The imports that we have used can be accessed on-line if you need more information on the parameters used in each instantiation. 
For more information regarding this please refer to: Android API

There is one last IMPORTANT detail that you need to take care if you don't want to find surprises. If you run that code as it is, you may notice that you get errors, and that is because you need to make a simple modification to the Manifest file. This file can be seen from Eclipse IDE as follows:


This is located in the "Package Explorer" in Eclipse. Open the AndroidManifest.xml and add the following code to the xml file:


<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

This code is needed by the device in order to add the necessary permissions to use the GPS or Network.

Now you are ready to deploy your application to your device and start collecting GPS data that can be used to create location-based services. This coded is intended to give a brief introduction that will give you a push in your journey to the futuristic location-based services. 





Uninstalling MySQL from Mac

Have you ever wanted to get rid of mysql from your Mac and discovered how tedious in may become to get rid of it? As a Windows user, we are used to install and uninstall any piece of software with just a click of a button. The robustness of a Mac as well as the power of the Mac Computers sometimes may be a burden, fortunately there is always ways to overcome the difficulties we find when using a Mac. This little post may help developers spend more time in their task instead of figuring out how to get rid of a piece of software.


Just execute the following commands o the Terminal in order to have MySQL completely removed from your Mac Computer:

shell> sudo rm /usr/local/mysql
shell> sudo rm -rf /usr/local/mysql*
shell> sudo rm -rf /Library/StartupItems/MySQLCOM
shell> sudo rm -rf /Library/PreferencePanes/My*
shell> sudo rm -rf /Library/Receipts/mysql*
shell> sudo rm -rf /Library/Receipts/MySQL*