2

am trying to create a class that will be used in acquiring the users Location. this class will be utilized by an IntentService in the background. is there a way to do this without extending Activity or FragmentActivity in my class. the code so far looks like below.

import android.app.IntentService;
import android.content.Context;
import static android.content.Context.LOCATION_SERVICE;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;

public class LocateMe    {
    // set loc Listener

    LocationManager locationManager;
    String update_locid, provider;
    double mLon, mLat;
    Float accuracy;
    int count = 0;
    Criteria criteria;
    Context context;
    IntentService is;
    onLocationGot mCallback;

    public LocateMe(onLocationGot ints){
        is = (IntentService) ints;
        // This makes sure that the container service has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (onLocationGot) ints;
        } catch (ClassCastException e) {
            throw new ClassCastException(ints.toString()
                    + " must implement LocateMe.onLocationGot");
        }
    }
    private LocationListener mLocationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
        // Getting latitude of the current myLocation
        double latitude = location.getLatitude();

        // Getting longitude of the current myLocation
        double longitude = location.getLongitude();

        // getting accuracy
        accuracy = location.getAccuracy();

        // Setting latitude and longitude to be used in the TextView 
        mLat = latitude;
        mLon = longitude;

        if (count > 5) {
            locationManager.removeUpdates(mLocationListener);
        }
        count++;

        mCallback.foundLocation(mLat, mLon, accuracy);
    //            finish();
        }

        public void onProviderDisabled(String provider) {// more work required here
            // str = provider;
        }

        public void onProviderEnabled(String provider) {
            // str = provider;
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
            provider = locationManager.getBestProvider(criteria, true);
        }
    };

//    @Override
//    public void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//    }

    public void getLocation() {
        // Getting Google Play availability status
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(is.getBaseContext(        ));

    // Showing status
    if (status != ConnectionResult.SUCCESS) { // Google Play Services are not available

//            int requestCode = 10;
//            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
//            dialog.show();

        } else {
            // Getting LocationManager object from System Service LOCATION_SERVICE
            locationManager = (LocationManager) is.getSystemService(LOCATION_SERVICE);

        // Creating a criteria object to retrieve provider
        criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        //criteria.setPowerRequirement(Criteria.POWER_LOW);
        // Getting the name of the best provider
        provider = locationManager.getBestProvider(criteria, true);

        // Getting Current Location or on editing saved location
        if (provider != null) {
            requestLocation(locationManager, provider);
        } else {
            //start wifi, gps, or tell user to do so
        }
    }
}

    public void requestLocation(LocationManager locationManager, String provider) {

        if (provider != null) {
            locationManager.requestLocationUpdates(provider, 1000, 0, mLocationListener);
        } else {
            //tell user what has happened
            }
        }

        public interface onLocationGot {

            public void foundLocation(double lat, double lon, Float accuracy);
         }
    }
hyena
  • 755
  • 1
  • 14
  • 24

1 Answers1

0

I suspect that your question is if you can get LocationService in non-activity class. If so, then read below.

To access system services you need to call this method. Activity extends Context, so it's able to get system services. IntentService also extends Context, so you can use your constructor param to get location service.

  • Ok so this means the code will work as is since am passing IntentService to the class. – hyena Oct 23 '14 at 15:39
  • what about the following line, Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode); it requires an activity as one of its parameters – hyena Oct 23 '14 at 15:41
  • i have realized the dialog will not work, am going to use a toast – hyena Oct 23 '14 at 16:36