LocationManager在服务中

时间:2012-11-12 13:55:28

标签: android gps location

是否有可能在服务中使用getSystemService?我的服务类:

private LocationManager locationManager;
private LocationListener locationListener;

IBinder mBinder = new LocalBinder();      

public interface interfaceLocationService {     
      public Location currentBestLocation = null;
      public void startListenLocation();
      public void stopListenLocation();
      public Location getUserLocation();
}

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

public class LocalBinder extends Binder implements interfaceLocationService{

    public void StopListenLocation(){
        locationManager.removeUpdates(locationListener);
        locationListener = null;
        stopSelf();
    }

    public void startListenLocation()
    { 
        locationListener = new LocationListener() {

            public void onStatusChanged(String provider, int status, Bundle extras) {    
                switch (status) {

                // Provider out of service
                case LocationProvider.OUT_OF_SERVICE:
                    Log.v(TAG, "Provider status changed: Out of Service");
                    break;

                // Provider temporarily unavailable
                case LocationProvider.TEMPORARILY_UNAVAILABLE:
                    Log.v(TAG, "Provider status changed: Temporarily Unavailable");
                    break;

                // Provider available again
                case LocationProvider.AVAILABLE:
                    Log.v(TAG, "Provider status changed: Available");
                    break;
                }
            }       

            public void onProviderEnabled(String provider) {    
                Log.v(TAG, "Provider enabled");
            }

            public void onProviderDisabled(String provider) {    
                Log.v(TAG, "Provider disabled");

                // Settings brought up to connect
                Intent intent = new Intent(
                        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }

            public void onLocationChanged(Location location) {                  
                Log.v(TAG, "Location Changed");
                if (isBetterLocation(location, currentBestLocation)) {
                    currentBestLocation.set(location);
                }
            }
        };
        //if I have only one requestLocationUpdates situation is the same
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
                    400, 1, locationListener);
        }

    public Location getUserLocation(){
        return currentBestLocation;
    }

    @Override
    public void stopListenLocation() {
        locationManager.removeUpdates(locationListener);
        locationListener = null;
        stopSelf();
    }
}

@Override
public void onCreate() {
    // Start thread  
    locationManager = (LocationManager)SendService.this.getSystemService(Context.LOCATION_SERVICE);
    super.onCreate();
    Log.d(TAG, "SendService created");
}

@Override
public void onDestroy() {
    // Just destroy thread
    super.onDestroy();
    Log.d(TAG, "SendService destroyed");
}

在活动中,我得到了启动此服务的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    Intent intent = new Intent(this, SendService.class);
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

private ServiceConnection mConnection = new ServiceConnection() {

    public void onServiceConnected(ComponentName className,
            IBinder service) {
        mService = (interfaceLocationService) service; 
    }

    public void onServiceDisconnected(ComponentName arg0) {
        Log.d("LOG","Service disconnected");
        mService.stopListenLocation();
    }
};

public void startClicked(View v) {
    mService.startListenLocation();   
}

此代码在LogCat中出错:

11-12 17:01:02.171: E/AndroidRuntime(2719): FATAL EXCEPTION: main
11-12 17:01:02.171: E/AndroidRuntime(2719): android.util.AndroidRuntimeException:      Calling startActivity() from outside of an Activity  context requires the      FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
11-12 17:01:02.171: E/AndroidRuntime(2719):     at android.app.ContextImpl.startActivity(ContextImpl.java:657)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at com.example.gps.data.SendService$LocalBinder$1.onProviderDisabled(SendService.java:78)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:204)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:124)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:140)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at android.os.Looper.loop(Looper.java:143)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at android.app.ActivityThread.main(ActivityThread.java:4717)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at java.lang.reflect.Method.invokeNative(Native Method)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at java.lang.reflect.Method.invoke(Method.java:521)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at dalvik.system.NativeStart.main(Native Method)

简单地说:我可以在后台使用LocationManager收听service中的位置吗?如果是,那么我的错误在哪里?

1 个答案:

答案 0 :(得分:1)

是的,您当然可以在服务中使用LocationManager。例如,我在后台服务的onCreate方法中执行此操作:

public void onCreate() {
      mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

      Criteria criteria = new Criteria();
      criteria.setAccuracy(Criteria.ACCURACY_COARSE);
      criteria.setAltitudeRequired(false);
      criteria.setBearingRequired(false);
      criteria.setCostAllowed(true);

      String provider = mLocationManager.getBestProvider(criteria, true);
      mLocationManager.requestLocationUpdates(provider, LOCATION_UPDATE_INTERVAL_MILLIS, LOCATION_UPDATE_INTERVAL_METERS, this);

    }