2013-01-25 64 views
0

我需要你的幫助。 我有一個活動,每5分鐘獲得當前的經緯度gps = new GpsData(StartActivity.this);。這工作正常!但是當我離開一個縣並且活動更新時。然後,我沒有得到目前的最大拉特。我不知道爲什麼。android:locationManager onLocationChanged不更新活動

下面是代碼:

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.listplaceholder); 

     btnShowLocation = (Button) findViewById(R.id.report_btn); 
     btnShowLocation.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       Intent intent = new Intent(StartActivity.this, ReportActivity.class); 
       startActivity(intent); 
       finish(); 

      } 
     }); 

    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     autoUpdate = new Timer(); 
     autoUpdate.schedule(new TimerTask() { 
     @Override 
     public void run() { 
     runOnUiThread(new Runnable() { 
     public void run() { 
      //start GPS 
      gps = new GpsData(StartActivity.this); 
      //start json download 
      new task().execute(); 
     } 
     }); 
     } 
     }, 0, 300000); // updates each 5 min 
    } 

class task extends AsyncTask<String, String, Void> 
{ 
private ProgressDialog progressDialog = new ProgressDialog(StartActivity.this); 
    InputStream is = null ; 
    String result = ""; 
    protected void onPreExecute() { 
     progressDialog.setMessage("Status Update..."); 
     progressDialog.show(); 
     progressDialog.setOnCancelListener(new OnCancelListener() { 
    @Override 
     public void onCancel(DialogInterface arg0) { 
     task.this.cancel(true); 
     } 
    }); 
    } 
     @Override 
    protected Void doInBackground(String... params) { 
      //get Location Data 
       double latitude = gps.latitude; 
       double longitude = gps.longitude; 
       String mlat = String.valueOf(latitude); 
       String mlon = String.valueOf(longitude); 

       //if (gps.latitude != 0.0) { 

      JSONObject json = jsonFunctions.getJSONfromURL("http://myurl); 

這裏是我的GpsData

public class GpsData extends Service implements LocationListener {  
public GpsData(Context context) { 
      this.mContext = context; 
      getLocation(); 
     } 

     public Location getLocation() { 

     // Get the location manager 
     locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); 

     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
     return null; 

     } 

     /* Request updates at startup */ 
     protected void onStart() { 

     } 

     /* Remove the locationlistener updates when Activity is paused */ 
     protected void onPause() { 
     locationManager.removeUpdates(this); 
     } 

     @Override 
     public void onLocationChanged(Location location) { 
      latitude = location.getLatitude(); 
      longitude = location.getLongitude(); 
      time = location.getTime(); 
      speed = location.getSpeed(); 
     } 

這很簡單:我想每5分鐘當前的緯度經度和刷新從當前數據列表視圖目前的縣...我怎麼能做到這一點?

回答

0

更改此

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 300000, 0, this);