1

我制作了一个简单的代码,允许用户在按下某个按钮时获取他当前的 GPS 位置。所以,我创建了一个 MainActivity 和一个 Asynctask 类,Asynctask 实现了 LocationListener 但永远不会调用覆盖 onLocationChanged !(LogCat 中没有任何痕迹..)然后,我得到 gps 数据,但当我按下按钮时他从未改变:/

如果我离开应用程序,如果我强制进程退出参数选项 Android 并再次启动我的应用程序,gps 数据保持不变。我不明白..为什么永远不会调用覆盖方法。

这是我唯一的文件:

公共类 MainActivity 扩展 Activity {

    public static Context context;
    public Button push = null;
    public getGPS tache_getGPS = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = getApplication().getApplicationContext();

        push = (Button) findViewById(R.id.button);
        push.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.i("T: onClick", "debut");
                tache_getGPS = new getGPS();
                tache_getGPS.execute();
                Log.i("T: onClick", "fin");
                // TODO Auto-generated method stub
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

class getGPS extends AsyncTask<Void, Integer, Location> implements
        LocationListener {

    final long REFRESH = 5 * 1000;
    private Location location;
    private LocationManager lm;

    protected void onPreExecute() {
        Log.i("T: onPreExcute", "debut");
        Criteria crit = new Criteria();
        crit.setAccuracy(Criteria.ACCURACY_FINE);

        // Configure location manager - I'm using just the network provider in
        // this example
        lm = (LocationManager) MainActivity.context
                .getSystemService(Context.LOCATION_SERVICE);

        String best = lm.getBestProvider(crit, false);
        Log.i("T: onPreExecute", "best : " + best);

        lm.requestLocationUpdates(best, 0, 1, this);
        location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        // nearProgress.setVisibility(View.VISIBLE);
        Log.i("T: onPreExcute", "fin");
    }

    protected Location doInBackground(Void... params) {
        Log.i("T: doInBackground", "debut");

        // Try to use the last known position
        Location lastLocation = lm
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                /*
        // If it's too old, get a new one by location manager
        if (System.currentTimeMillis() - lastLocation.getTime() > REFRESH) {
            while (location == null)
                try {
                    Thread.sleep(100);
                } catch (Exception ex) {
                }

            return location;
        }
                */
        Log.i("T: doInBackground", "fin");
        return lastLocation;
    }

    protected void onPostExecute(Location location) {
        Log.i("T: onPostExecute", "debut");
        // nearProgress.setVisibility(View.GONE);

        lm = (LocationManager) MainActivity.context
                .getSystemService(Context.LOCATION_SERVICE);
        lm.removeUpdates(this);

        Log.i("T: onPostExecute",
                "Altitude : " + String.valueOf(location.getAltitude()));
        Log.i("T: onPostExecute",
                "Longitude : " + String.valueOf(location.getLongitude()));
        Log.i("T: onPostExecute",
                "Latitude : " + String.valueOf(location.getLatitude()));
        Log.i("T: onPostExecute",
                "Precision(mètre) : " + String.valueOf(location.getAccuracy()));
        Log.i("T: onPostExecute", "fin");

        Toast.makeText(
                MainActivity.context,
                "Altitude : " + String.valueOf(location.getAltitude()) + "\n"
                        + "Longitude : "
                        + String.valueOf(location.getLongitude()) + "\n"
                        + "Latitude : "
                        + String.valueOf(location.getLatitude()) + "\n"
                        + "Precision(mètre) : "
                        + String.valueOf(location.getAccuracy()),
                Toast.LENGTH_SHORT).show();

        return;
    }

    @Override
    public void onLocationChanged(Location newLocation) {
        Log.i("T: onLocationChanged", "debut");
        location = newLocation;
        Log.i("T: onLocationChanged", "fin");
    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.i("T: onProviderDisabled", provider);
    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.i("T: onProviderEnabled", provider);
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.i("T: onStatusChanged", "provider : " + provider);
        Log.i("T: onStatusChanged", "status : " + status);
        Log.i("T: onStatusChanged", "extras : " + extras.toString());
    }

}

感谢您的帮助,并为我糟糕的英语写作感到抱歉 x)

新代码(在建议 =) 之后),没有 Asinctask

public class MainActivity extends Activity implements LocationListener{

    public static Context context;
    public Button push = null;
    public getGPS tache_getGPS = null;

    private Location location;
    private LocationManager lm;
    final long REFRESH = 5 * 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = getApplication().getApplicationContext();

        push = (Button) findViewById(R.id.button);
        push.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.i("T: onClick", "debut");
                location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                Location lastLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                if (System.currentTimeMillis() - lastLocation.getTime() > REFRESH) {
                     while (location == null) 
                         try { Thread.sleep(100); } catch (Exception ex) { }
                     Log.i("FINAL : location", location.toString());
                     Toast.makeText(MainActivity.context, "location : "+location.toString(), Toast.LENGTH_SHORT).show();
                     return;
                }
                Log.i("FINAL : lastlocation", lastLocation.toString());
                Toast.makeText(MainActivity.context, "lastLocation : "+lastLocation.toString(), Toast.LENGTH_SHORT).show();
                Log.i("T: onClick", "fin");
            }
        });
    }

    @Override
    protected void onResume(){
        super.onResume();
        Criteria crit = new Criteria();
        crit.setAccuracy(Criteria.ACCURACY_FINE);
        lm = (LocationManager) MainActivity.context.getSystemService(Context.LOCATION_SERVICE);

        String best = lm.getBestProvider(crit, false);
        Log.i("T: onPreExecute", "best : " + best);

        lm.requestLocationUpdates(best, 1, 1, this);
    }

    @Override
    protected void onPause(){
        super.onPause();
        lm.removeUpdates(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onLocationChanged(Location newLocation) {
        Toast.makeText(context, "onLocationChanged", Toast.LENGTH_SHORT).show();
        Log.i("T: onLocationChanged", "debut");
        location = newLocation;
        Log.i("T: onLocationChanged", "fin");
    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.i("T: onProviderDisabled", provider);
    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.i("T: onProviderEnabled", provider);
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.i("T: onStatusChanged", "provider : " + provider);
        Log.i("T: onStatusChanged", "status : " + status);
        Log.i("T: onStatusChanged", "extras : " + extras.toString());
    }

}
4

1 回答 1

0

requestLocationUpdates您指定 1 作为 minDistance (minDistance 是通知的最小距离间隔,以米为单位)。尝试将其设置为 0。

于 2013-03-10T15:59:47.947 回答