我尝试这样做 Android LocationRequest: get a callback when request expires
在片段中我有这段代码
@NonNull
protected Handler sHandler = new Handler(CoreApp.getContext().getMainLooper());
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
sHandler.postDelayed(mExpiredRunnable, 5000);
}
final Runnable mExpiredRunnable = new Runnable() {
@Override
public void run() {
System.out.println("aaa");
}
};
片段,一切正常。我有LocationHelper类,该类具有使用位置的方法。在locationHelpetr中,我有此代码
@NonNull
protected Handler sHandler = new Handler(CoreApp.getContext().getMainLooper());
final Runnable mExpiredRunnable = new Runnable() {
@Override
public void run() {
System.out.println("aaa konec");
mOnSuccessLocationListener.onSuccess(null);
}
};
public void startLocationUpdates() {
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setInterval(5000);
locationRequest.setFastestInterval(5000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setExpirationDuration(15000);
mFusedLocationClient.requestLocationUpdates(locationRequest, mLocationCallback, null);
if (mListener != null) {
mFusedLocationClient.getLastLocation().addOnSuccessListener(mOnSuccessLocationListener);
sHandler.postDelayed(mExpiredRunnable, 5000);
}
}
mExpireRunnable从未在locationHelper中调用。片段中一切正常。