是否可以在非活动类中使用locationManager

时间:2014-10-23 15:09:17

标签: android location locationmanager android-intentservice

我正在尝试创建一个用于获取用户位置的类。此类将在后台由IntentService使用。 有没有办法在不扩展Activity或FragmentActivity的情况下执行此操作 在我班上 到目前为止,代码如下所示。

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);
         }
    }

1 个答案:

答案 0 :(得分:0)

我怀疑你的问题是你是否可以在非活动类中获得LocationService。 如果是,请阅读以下内容。

要访问系统服务,您需要调用this方法。 Activity扩展了Context,因此它能够获得系统服务。 IntentService还扩展了Context,因此您可以使用构造函数参数来获取位置服务。