Android 安卓GPS定位系统-它没有';不要转到onLocation已更改

Android 安卓GPS定位系统-它没有';不要转到onLocation已更改,android,gps,Android,Gps,我想获取GPS位置值。我已经编写了代码。但我没有显示消息。我已经在devicenotemulator中进行了测试。设备包含GPS及其启用也没有互联网连接 请告诉我我的代码出了什么问题 public class AndroidGPSSampleActivity extends Activity { private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters private stat

我想获取GPS位置值。我已经编写了代码。但我没有显示消息。我已经在devicenotemulator中进行了测试。设备包含GPS及其启用也没有互联网连接

请告诉我我的代码出了什么问题

public class AndroidGPSSampleActivity extends Activity {

    private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
    private static final long MINIMUM_TIME_BETWEEN_UPDATES = 30000; // in Milliseconds
    protected LocationManager locationManager;
    protected Button retrieveLocationButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            MINIMUM_TIME_BETWEEN_UPDATES, 
            MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
            new MyLocationListener()
        );
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new MyLocationListener());

        retrieveLocationButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showCurrentLocation();
            }
        });
    }

    protected void showCurrentLocation() {

        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location != null) {
            String message = String.format(
                    "Current Location \n Longitude: %1$s \n Latitude: %2$s",
                    location.getLongitude(), location.getLatitude()
            );

            Toast.makeText(AndroidGPSSampleActivity.this, "Sample test",
                Toast.LENGTH_LONG).show();
            Toast.makeText(AndroidGPSSampleActivity.this, message,
                Toast.LENGTH_LONG).show();
        }
    }

    private class MyLocationListener implements LocationListener {
        public void onLocationChanged(Location location) {
            String message = String.format(
                "New Location \n Longitude: %1$s \n Latitude: %2$s",
                location.getLongitude(), location.getLatitude()
            );
            Toast.makeText(AndroidGPSSampleActivity.this, message, Toast.LENGTH_LONG).show();
        }

        public void onStatusChanged(String s, int i, Bundle b) {
            Toast.makeText(AndroidGPSSampleActivity.this, "Provider status changed",Toast.LENGTH_LONG).show();
        }

        public void onProviderDisabled(String s) {
            Toast.makeText(AndroidGPSSampleActivity.this,"Provider disabled by the user. GPS turned off",Toast.LENGTH_LONG).show();
        }

        public void onProviderEnabled(String s) {
            System.out.println("==onProviderEnabled=" + s);
            Toast.makeText(AndroidGPSSampleActivity.this, "Provider enabled by the user. GPS turned on",Toast.LENGTH_LONG).show();
        }
    }
和许可

      <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
     <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> 
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

始终
Location
提供空值。它没有转到
onLocationChanged
方法


请帮我解决这个问题…

我适应我在哪里教程。。希望能有所帮助

p、 我没有经过测试就写了。。从技术上讲,这是可行的。。我想

private Location loca;

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LocationManager locman;
        String context = Context.LOCATION_SERVICE;
        locman = (LocationManager)getSystemService(context);

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        String  provider = locman.getBestProvider(criteria, true);

        loca = locman.getLastKnownLocation(provider);

        updateWithNewLocation(loca);

        locman.requestLocationUpdates(provider, 1000, 1, locationListener);


        retrieveLocationButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
           String message = "My location : " + updateWithNewLocation(loca);

        Toast.makeText(AndroidGPSSampleActivity.this, message,
                Toast.LENGTH_LONG).show();
        }
});        


    }

    private final LocationListener locationListener = new LocationListener() {

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderDisabled(String provider) {
            updateWithNewLocation(null);
        }

        @Override
        public void onLocationChanged(Location location) {
            updateWithNewLocation(location);
        }
    };


    private string updateWithNewLocation(Location location){

        if(location!=null){

            double lat = location.getLatitude();
            double lng = location.getLongitude();
            latLongString = "Lat:" + lat + "\nLong:" + lng;

        }else{
            latLongString = "No location found";

        }
           return latLongString;

    }

我更改了你的一些代码请检查一下

 public class AndroidGPSSampleActivity extends Activity implements LocationListener{

 private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
 private static final long MINIMUM_TIME_BETWEEN_UPDATES = 30000; // in Milliseconds
 protected LocationManager locationManager;
 protected Button retrieveLocationButton;
 @Override
 public void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);

      locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
      Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

      locationManager.requestLocationUpdates(locationManager.NETWORK_PROVIDER, 500L, 250.0f, this);

      retrieveLocationButton.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
              showCurrentLocation();
          }
      });        

}    


        @Override
    public void onPause() {
            super.onPause();
            locationManager.removeUpdates(this);
    } 

    @Override
    public void onDestroy(){
        locationManager.removeUpdates(this);
        super.onDestroy();
    }

        protected void showCurrentLocation() {

            Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if(location == null){
            location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            }   
            if (location != null) {
                String message = String.format(
                        "Current Location \n Longitude: %1$s \n Latitude: %2$s",
                        location.getLongitude(), location.getLatitude()
                );

                Toast.makeText(AndroidGPSSampleActivity.this, "Sample test",
                        Toast.LENGTH_LONG).show();
                Toast.makeText(AndroidGPSSampleActivity.this, message,
                        Toast.LENGTH_LONG).show();
            }

        }   

        public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        if (location != null){
            String message = String.format("New Location \n Longitude: %1$s \n Latitude: %2$s",
                               location.getLongitude(), location.getLatitude());
                        Toast.makeText(AndroidGPSSampleActivity.this, message, Toast.LENGTH_LONG).show();
                }
    }

    public void onStatusChanged(String s, int i, Bundle b) {
                Toast.makeText(AndroidGPSSampleActivity.this, "Provider status changed",Toast.LENGTH_LONG).show();
        }

        public void onProviderDisabled(String s) {
               Toast.makeText(AndroidGPSSampleActivity.this,"Provider disabled by the user. GPS turned off",Toast.LENGTH_LONG).show();
        }

        public void onProviderEnabled(String s) {
               System.out.println("==onProviderEnabled=" + s);
               Toast.makeText(AndroidGPSSampleActivity.this, "Provider enabled by the user. GPS turned on",Toast.LENGTH_LONG).show();
        }

}

您使用模拟位置进行测试,不是吗?如果是这样,我不确定我能帮你多少忙,因为我对模拟数据了解不多。你在真实设备上测试过吗?