【问题标题】:is it posible to use locationManager in a non-activity class是否可以在非活动类中使用 locationManager
【发布时间】:2014-10-23 15:09:16
【问题描述】:

我正在尝试创建一个用于获取用户位置的类。此类将由 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);
         }
    }

【问题讨论】:

标签: android location locationmanager android-intentservice


【解决方案1】:

我怀疑您的问题是您是否可以在非活动类中获得 LocationService。 如果是这样,请阅读下文。

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

【讨论】:

  • 好的,这意味着代码将按原样工作,因为我将 IntentService 传递给类。
  • 下面这行怎么样,Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);它需要一个活动作为其参数之一
  • 我已经意识到对话框将无法正常工作,我将使用 toast
猜你喜欢
  • 2011-06-19
  • 1970-01-01
  • 1970-01-01
  • 2015-04-05
  • 2011-12-01
  • 1970-01-01
  • 2015-04-14
  • 1970-01-01
相关资源
最近更新 更多