2012-02-13 31 views
0

我想獲得GPS位置值。我寫了code.But我沒有顯示消息。我已經在deveice進行了測試,不是emulator.Device包含GPS &其啓用。 &也沒有互聯網連接。Android GPS位置 - 它不會去onLocationChanged

請告訴我什麼是錯在我的代碼

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方法...

請幫我從這個問題...

+0

您使用模擬地點進行測試,不ü?如果是這樣,我不知道我可以幫助你,因爲我對模擬數據瞭解不多。你有沒有在真實的設備上測試? – 2012-02-13 08:57:54

回答

1

我適應我在哪裏教程..希望這將有助於

附:我把它寫未經測試..從技術上說,它的工作..我猜

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; 

    } 
0

我改變了一些代碼的請這個

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(); 
     } 

}