0

私はこの問題を解決しようとしていonProgressUpdate()ます。メソッドに送信される座標は0.0に等しくなります。デバッガーを使用して、メソッドが0.0getLatitude()を返すことを発見しました。getLongitude()問題は、これらのメソッド、それらのクラスが大丈夫だったということです(私は以前にそれを使用しました)。簡略化されたコードは次の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 に答える