服务在后台和界面没关系啊,你是不是在activity  onpause的时候把服务关了啊

解决方案 »

  1.   

    服务是bind的还是startService的?如果是bind的Activity finish后Service就停止并且释放了。
      

  2.   

    public class ServiceLocationGPS extends Service implements AMapLocationListener { private GlobalApp app;
    private LocationManagerProxy aMapLocManager = null;
    private AMapLocation aMapLocation;// 用于判断定位超时 public Timer timer;
    TimerTask task;
    public Integer period = 10;
    public Integer count = 0; @Override
    public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
    } @Override
    public void onCreate() {
    app = (GlobalApp) getApplication();
    start();
    super.onCreate();
    } @Override
    public void onDestroy() {
    stoptime();
    super.onDestroy();
    } @Override
    @Deprecated
    public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);
    } @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    return super.onStartCommand(intent, flags, startId);
    } @Override
    public void onLocationChanged(Location arg0) {
    // TODO Auto-generated method stub } @Override
    public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub } @Override
    public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub } @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub } public void startLocation() {
    if (aMapLocManager == null) {
    aMapLocManager = LocationManagerProxy
    .getInstance(ServiceLocationGPS.this);
    System.out.println("startLocation");
    aMapLocManager.requestLocationUpdates(
    LocationProviderProxy.AMapNetwork, -1, 10, this);

    } else {
    aMapLocManager.requestLocationUpdates(
    LocationProviderProxy.AMapNetwork, -1, 10, this);

    } public void start() {
    initSchedule();
    // 10秒以后启动任务;
    startSchedule(10);
    } @Override
    public void onLocationChanged(AMapLocation location) {

    if (location != null) {
    this.aMapLocation = location;// 判断超时机制
    Double geoLat = location.getLatitude();
    Double geoLng = location.getLongitude();
    String cityCode = "";
    String desc = "";
    Bundle locBundle = location.getExtras();
    if (locBundle != null) {
    cityCode = locBundle.getString("citycode");
    desc = locBundle.getString("desc");
    }
    System.out.println("onLocationChanged," + geoLat + "," + geoLng);
    // aMapLocManager.removeUpdates(this);
    //stopLocation();
    } } /**
     * 销毁定位
     */
    public void stopLocation() {
    if (aMapLocManager != null) {
    aMapLocManager.removeUpdates(this);
    aMapLocManager.destory();
    }
    aMapLocManager = null;
    } /**
     * 开启定时任务;
     */
    public void startSchedule(int delay) {
    if (timer != null && task != null)
    // 10秒以后执行任务;
    timer.schedule(task, delay * 1000, period * 1000);
    } public void initSchedule() {
    if (timer == null)
    timer = new Timer();
    if (task == null)
    task = new TimerTask() {
    @Override
    public void run() {
    startLocation();
    }
    };
    } private void stoptime() {
    if (timer != null) {
    timer.cancel();
    timer = null;
    }
    if (task != null) {
    task.cancel();
    task = null;
    }
    }}
    主要是调用了其他Activity之后 onLocationChanged 不进行回调了。 
    如果不调用其他Activity 那么 onLocationChanged 就不停在打印。
    找不到原因。
      

  3.   

    看代码只是Service的部分,还是不清楚你跳转其他Activity的方法和你开启和关闭服务的方法。。但是估计就这两个地方,或者是你的定时器问题了。