2016-09-19 64 views

回答

1
public class MyActivity extends AppCompatActivity implements LocationListener { 

    private LocationManager locationManager; 

    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     locationManager.removeUpdates(this); 
    } 

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

    } 

    @Override 
    public void onProviderEnabled(String provider) { 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 

    } 

} 

這裏是參考LocationListener的接口。

如果你只想要一個位置更新,那麼你可以調用

locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER,這一點,Looper.getMainLooper());

+0

謝謝你這工作:) – atanti

相關問題