我希望locationlistener在更改位置时停止使用gps,这可能吗?
答案 0 :(得分: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,this,Looper.getMainLooper());