0

我正在尝试解决这个问题:发送到onProgressUpdate()方法的坐标等于 0.0。getLatitude()使用调试器,我发现方法getLongitude()返回 0.0。问题是,这些方法,它们的类没问题(我以前用过)。这是的简化代码MainActivity

public class MainActivity extends Activity {

Button btnStart;
MyTask objMyTask;

@Override
public void onCreate(Bundle savedInstanceState) {
    ...

    btnStart = (Button) findViewById(R.id.btnstart);
    btnStart.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            objMyTask = new MyTask();
            objMyTask.execute();
        }
    });
}

class MyTask extends AsyncTask<Void, Double, Integer> {
GPSTracker gps;
    @Override
    protected Integer doInBackground(Void... params) 
    {
        Double latitude, longitude;
        long t = System.currentTimeMillis()-6000;
        long currentTime = t;
        int i = 0;
        gps = new GPSTracker(MainActivity.this);
        while(i < 5)
        {
            currentTime = System.currentTimeMillis();
            if((currentTime - t) >= 5000)
            {
                i++;
                if(gps.canGetLocation()){
                    latitude = gps.getLatitude();
                    longitude = gps.getLongitude();
                    Double[] resultArray = new Double[2];
                    resultArray[0] = latitude;
                    resultArray[1] = longitude;
                    publishProgress(resultArray);
                }else{
                    gps.showSettingsAlert();
                }
                t = System.currentTimeMillis();
            }
        }
        return 0;
    }

    @Override
    protected void onProgressUpdate(Double... values) {     
        super.onProgressUpdate(values);
        Toast.makeText(getApplicationContext(), "Latitude: "+values[0]+"\nLongitude"+values[1], Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onPostExecute(Integer result) {
        ...
    }

}

}

我认为问题出在 的构造函数中GPSTracker,也许无法从我创建的这个线程访问上下文。这是构造函数:

    public GPSTracker(Context context) {
    this.mContext = context;
    getLocation();
}

我已经没有办法解决这个问题了。在此先感谢您的帮助。

4

1 回答 1

1
public GPSTracker(Context context) { 
    this.mContext = context.getApplicationContext(); 
    getLocation();
}
于 2013-02-25T22:44:21.257 回答