Question

In my application I am trying to find the user's location after a fixed interval of time if the user has closed the application with the help of broadcast.

However in my Broadcast Receiver class I am getting the desired Latitude & Longitude of the user's location. But how can I use that lat and long to find the user's address with the help of it.

The geocoder requires activity context, however I have a class extending broadcast Receiver.

public class LocationReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle b=intent.getExtras();
        Location loc=(Location)b.get(LocationPoller.EXTRA_LOCATION);

        // Getting latitude
        double latitude = loc.getLatitude();

        // Getting longitude
        double longitude = loc.getLongitude();
        System.out.println( loc.getLatitude());
        System.out.println(     loc.getLongitude());

        addGet(latitude,longitude) 
    }

    public void addGet(double latitude,double longitude) {

        Geocoder  geocoder = new Geocoder(this, Locale.getDefault());

        try
        {
            List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
            Log.e("Addresses","-->"+addresses);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
Was it helpful?

Solution

Pass the context.

    addGet(context, latitude,longitude);

    public void addGet(Context context,double latitude,double longitude) {
       Geocoder  geocoder = new Geocoder(context, Locale.getDefault());
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top