package com.example.androidserver;import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Timer;
import java.util.TimerTask;import org.json.JSONException;
import org.json.JSONObject;import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.PowerManager.WakeLock;
import android.util.Log;
 
public class GPSService extends Service implements LocationListener {
 
    //2000ms
    private static final long minTime = 1;
    //最小变更距离 10m
    private static final float minDistance = 0; 
    private LocationManager locationManager;
    private LocationListener locationListener;
 
    private final IBinder mBinder = new GPSServiceBinder();
    private static final String tag = "unity";
    
    public Location previousBestLocation = null;    Intent intent;
    int counter = 0;
    private String intentEvent="gps.message";
    
    WakeLock wakeLock;    
    private  Timer mTimer =  null ;  
    private  TimerTask mTimerTask =  null ; 
    private static final int UPDATE_TEXTVIEW = 0;  
    
    private static int count = 0;  
    private boolean isPause = false;  
    private boolean isStop = true;  
    private static int delay = 1000;  //1s  
    private static int period = 1000;  //1s  
    
    String provider;
    
    public void startService()
    {
      locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
     locationListener=this;
    
      Criteria criteria = new Criteria();  
        criteria.setAccuracy(Criteria.ACCURACY_FINE);  
        criteria.setAltitudeRequired(false);  
        criteria.setBearingRequired(false);  
        criteria.setCostAllowed(true);  
        criteria.setPowerRequirement(Criteria.POWER_LOW);  
        provider = locationManager.getBestProvider(criteria, true);
        intent=new Intent(intentEvent);
        intent.putExtra("debug", provider);
        sendBroadcast(intent);        locationManager.requestLocationUpdates(provider, minTime, minDistance, locationListener);
        Log.v(tag, provider+"----StartService");
    }
 
    public void endService()
    {
     intent=new Intent(intentEvent);
        intent.putExtra("debug", "GPSService Ended");
        sendBroadcast(intent);
        
      if(locationManager != null && locationListener != null)
         {
             locationManager.removeUpdates(locationListener);
         }
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
     Log.v("unity", "onStartCommand");
     flags = START_STICKY;  
        return super.onStartCommand(intent, flags, startId);  
    };
 
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return mBinder;
    }
 
    @Override
    public void onCreate()
    {
     //startTimer();  
        startService();
    }
 
    @Override
    public void onDestroy()
    {
     stopTimer();  
     Log.v("unity", "OnDestroy");
        endService();
        
        super.onDestroy();
    }
 
    public class GPSServiceBinder extends Binder {
        GPSService getService() {
            return GPSService.this;
        }
    }
    
    private static final float minAccuracyMeters = 35;
    private final DateFormat timestampFormat = new SimpleDateFormat("yyyyMMddHHmmss");
   
@Override
public void onLocationChanged(Location location) {        if(location != null)
        {
          //  if (location.hasAccuracy() && location.getAccuracy() <= minAccuracyMeters)
          //  {
                //获取时间参数,将时间一并Post到服务器端                JSONObject gpsJson=new JSONObject();
                try{
                 gpsJson.put("state", "UpdateGPS");
                 gpsJson.put("latitude", Double.toString(location.getLatitude()));
                 gpsJson.put("longitude", Double.toString(location.getLongitude()));
                 gpsJson.put("altitude", Double.toString(location.getAltitude()));
                }catch(JSONException ex){
                 throw new RuntimeException();
                }
                
                intent=new Intent(intentEvent);
                intent.putExtra("gpsInfo", gpsJson.toString());              
                sendBroadcast(intent);  
          //  }
        }
} @Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
Log.v("unity", "onStatusChanged"+provider);
} @Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Log.v("unity", "onProviderEnabled"+provider);
} @Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Log.v("unity", "onProviderDisabled"+provider);
}

Location location_old;
 private void startTimer(){  
        if (mTimer == null) {  
            mTimer = new Timer();  
        }  
  
        if (mTimerTask == null) {  
            mTimerTask = new TimerTask() {  
                @Override  
                public void run() {  
                
                  Location location = locationManager.getLastKnownLocation(provider);  
                  if(location==null)
                  {
                       return;
                  }
                  if(location_old==null)
                  location_old=location;
                  else if(location_old.getAltitude()==location.getAltitude()&&location_old.getLatitude()==location.getLatitude()&&location_old.getLongitude()==location.getLongitude())
                  {
                  return;
                  }
                  
                  String locationStr=location.toString();
                   Log.i(tag, "count: "+String.valueOf(count)+" : "+locationStr);  
                       intent=new Intent(intentEvent);
                       intent.putExtra("gpsInfo", locationStr);              
                       sendBroadcast(intent);  
                    location_old=location;
                    count ++;    
                }  
            };  
        }  
  
        if(mTimer != null && mTimerTask != null )  
            mTimer.schedule(mTimerTask, delay, period);  
    }  
  
    private void stopTimer(){  
          
        if (mTimer != null) {  
            mTimer.cancel();  
            mTimer = null;  
        }  
  
        if (mTimerTask != null) {  
            mTimerTask.cancel();  
            mTimerTask = null;  
        }     
        count = 0;  
  
    }  
    
}android运行进入后台后,gps持续更新1分钟左右后不再更新,server依然正常运行,应用进入前台后gps正常更新,求各路大神指点指点。