Activity inherits from Context, so you don't need to specify a context
specifically anywhere. Eclipse will tell you the exact error if you
hover over the line, or use the window at the bottom.

Does that not work?

LocationManager manager = (LocationManager) getSystemService
(LOCATION_SERVICE);
String provider = manager.getBestProvider(new Criteria(), true);
Location location = manager.getLastKnownLocation(provider);
return location;



On May 21, 5:55 pm, Phil <tweek.3...@gmail.com> wrote:
> Sorry if this is a stupid question- I'm still pretty new to Android
> and have been completely unable to figure this out after much google
> searching and hunting around in the documentation. *Note: I've been
> coding in Eclipse with the Android SDK plugins, using the Google APIs
> 1.5 version.
>
> I'm working on a project involving accessing the GPS, so I'm writing a
> simple program that will do nothing more than access the current
> location in order to familiarize myself with the GPS code.
>
> Based on the examples I have found, I came up with the following code.
>
> package com.example.gps;
>
> import android.app.Activity;
> import android.content.Context;
> import android.os.Bundle;
> import android.widget.Toast;
> import android.location.Location;
> import android.location.LocationManager;
> import android.location.Criteria;
>
> public class HelloGps extends Activity {
>     /** Called when the activity is first created. */
>     @Override
>     public void onCreate(Bundle savedInstanceState) {
>         super.onCreate(savedInstanceState);
>         setContentView(R.layout.main);
>         Context context = getContext();
>
>         Location location = getLocation(context);
>
>                 Toast.makeText(this, "Location = " + location.getLatitude() + 
> " , "
> + location.getLongitude(), 0);
>     }
>
>     public static Location getLocation(Context context)
>     {
>         LocationManager manager = (LocationManager)
> context.getSystemService(Context.LOCATION_SERVICE);
>         String provider = manager.getBestProvider(new Criteria(),
> true);
>         Location location = manager.getLastKnownLocation(provider);
>         return location;
>     }
>
> }
>
> The issue I am having deals with the "Context context = getContext()"
> line. Eclipse tells me that this is an error (but not why). It simply
> tells me I should change it to getBaseContext(). If I try this, then
> the program hangs. If I try removing it and running the getLocation()
> function without the context thrown in there at all, as I have seen in
> some other examples, then I get a similar error on the getSystemService
> () function call. Eclipse tells me I should rename it, but does not
> tell me why or what to.
>
> Any help at all would be incredible, and sorry again if this is a
> stupid question. Thanks!
>
> -Phil
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to