android:locationManager onLocationChanged不更新Activity

时间:2013-01-25 10:56:21

标签: android locationmanager

我需要你的帮助。 我有一个活动,每5分钟获得当前的纬度gps = new GpsData(StartActivity.this);。这很好用!但是当我离开一个县并且活动恢复时。然后我没有得到当前的lon lat。我不知道为什么。

以下是代码:

@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分钟一次当前的lat lon并使用当前县的当前数据刷新List视图......我该怎么做?

1 个答案:

答案 0 :(得分:0)

更改此

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

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