开发者

Getting GPS data?

Inside

public class IAmHere extends Activity implements LocationListener {

i have

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

}

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

}

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

}

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

}

and

inside

 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.iamhere);

i have

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    
    List<String> providers = lm.getProviders(true);  

    /* Loop over the array backwards, and if you get an accurate location, then break out the loop*/  
     Location l = null;  

    for 开发者_如何学JAVA(int i=providers.size()-1; i>=0; i--) {  
            l = lm.getLastKnownLocation(providers.get(i));  
            if (l != null) break;  
        }  

      double[] gps = new double[2];  
      if (l != null) {  
          gps[0] = l.getLatitude();  
          gps[1] = l.getLongitude();  
      }  

      gpsString = (TextView)findViewById(R.id.gpsString);

  String Data = ""; 
  String koordinata1 = Double.toString(gps[0]);
  String koordinata2 = Double.toString(gps[1]);
  Data = Data + koordinata1 + " | " + koordinata2 + "\n";
  gpsString.setText(String.valueOf(Data));

but seems it's not working? Why? I mean even emulator doesn't want to send GPS data - When I click "send" via UI or console, nothing happens...?

Thank you.


First, you implemented LocationListener on IAmHere, then did nothing with that interface.

Second, you are calling getLastKnownLocation(), but you are doing nothing to trigger Android to start collection location data on any provider.

These two problems are related.

Location services are off until something registers to get location data from them. Typically, this is via requestLocationUpdates() on LocationManager...which uses the LocationListener interface you implemented.

The recipe for using LocationManager is:

  1. Register a LocationListener via requestLocationUpdates()
  2. Unregister the LocationListener sometime (e.g., onDestroy()), so that you do not leak memory
  3. When location fixes come into onLocationChanged(), do something useful

Here is a sample project that uses LocationManager and LocationListener in this fashion.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜