1

我有这个问题。我正在获取位置更新。它在我的模拟器上运行良好。但是当我在我的设备上测试它时,它显示当前位置很好。但是当我改变我的位置时,它不会显示我的位置更新。但它在模拟器上运行良好。这是我的代码 Main.java

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity {

    TextView tvStatus;
    LocationManager lm;
    boolean gpsEnabled;
    LocationListener ls = new LocationListener() {

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {}

        @Override
        public void onProviderEnabled(String provider) {}

        @Override
        public void onProviderDisabled(String provider) {}

        @Override
        public void onLocationChanged(Location location) {
            tvStatus.setText("Latitude: "
                    + location.getLatitude() + "\nLongitude: "
                    + location.getLongitude());
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast.makeText(this, "on create", 300).show();
        tvStatus = (TextView) findViewById(R.id.textView1);
        lm = (LocationManager) getSystemService(LOCATION_SERVICE);
    }
    @Override
    protected void onResume() {
        super.onResume();
        Toast.makeText(this, "on resume", 200).show();
        if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            Location l = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            tvStatus.setText(l.getLatitude()+" "+ l.getLongitude());
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ls);
        }else{
            //use network provider
            Toast.makeText(this, "dsfdsfdsfdsf", 300).show();
        }
    }
    @Override
    protected void onPause() {
        super.onPause();
        Toast.makeText(this,"on pause" ,300).show();
        lm.removeUpdates(ls);
    }

}

我在 manifest.xml 中添加了所有权限

4

2 回答 2

0

首先,确保您的 gps 已启用并且信号已锁定/固定在实际设备上。如果问题仍然存在,则在 OnCreate() 中而不是在课堂上初始化您的 locationlistener。

于 2013-02-05T06:24:56.323 回答
0

If you are inside a building my suggestion is not to use GPS_PROVIDER as gps doesn't work much inside the building or takes large amount of time while taking the location fixes.

于 2013-02-05T06:51:27.930 回答