Android Tutorial

(avery) #1

By : Ketan Bhimani


74 


  1. Manually enter the longitude and latitude of your location. (Note they are in
    reverse order.)

  2. Click Send.


Back in the emulator, notice that the Google Map now shows the
location you seeded. Your screen should now display your location
as Yosemite Valley, as shown in Figure.

Your emulator now has a simulated location.

Finding the Last Known Location

To add location support to MyFirstAndroidApp, edit the file My First
AndroidApp .java. First, you must add the appropriate import
statements:

import android.location.Location;
import android.location.LocationManager;


Now, create a new method called getLocation() in your class and
make a call to this method in your onCreate() method. The
getLocation() method gets the last known location on the phone
and logs it as an informational message. If the operation fails for
some reason, the method logs an error.

The getLocation() method should look something like this:

public void getLocation() {
try {
LocationManager locMgr = (LocationManager)
getSystemService(LOCATION_SERVICE);
Location recentLoc = locMgr.
getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.i(DEBUG_TAG, “loc: “ + recentLoc.toString());
}
catch (Exception e) {
Log.e(DEBUG_TAG, “Location failed”, e);
}
}

Free download pdf