在的LocationManager服务 [英] LocationManager in service

查看:133
本文介绍了在的LocationManager服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible, to use getSystemService in service? My service class:

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

和活动中我得到了code启动该服务:

And in activity I got code starting this service:

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

这code给出LogCat中的错误:

This code gives an errors in 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 听位置服务,而在背景吗?如果是的话,哪里是我的错?

Simply: Could I use LocationManager to listen to location in service while in background? If yes, then where is my mistake?

推荐答案

是的,当然你可以在服务使用的LocationManager。

Yes of course you can use the LocationManager in a service. for instance I am doing this in the onCreate method of the background service :

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

    }

这篇关于在的LocationManager服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆