-2
我想讓locationlistener在位置改變時停止使用gps,這可能嗎?LocationListener如何在onLocationChanged()被調用時停止?
我想讓locationlistener在位置改變時停止使用gps,這可能嗎?LocationListener如何在onLocationChanged()被調用時停止?
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());
謝謝你這工作:) – atanti