I created a function for checking fence state in Awareness API. But I need to return the result. So I declared Boolean but it requires me to declare it to be final. Can you help me with solution how to return this value?
public static boolean isFencesActive(final Context context) {
boolean isActive;
Awareness.getFenceClient(context).queryFences(FenceQueryRequest.forFences(Arrays.asList(Constans.DETECTION_FENCE_DRIVING, Constans.DETECTION_FENCE_WALKING)))
.addOnSuccessListener(new OnSuccessListener<FenceQueryResponse>() {
@Override
public void onSuccess(FenceQueryResponse fenceQueryResponse) {
FenceStateMap map = fenceQueryResponse.getFenceStateMap();
isActive = !map.getFenceKeys().isEmpty(); //Needs to be final
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "Failed: " + e);
isActive = false;
}
});
return isActive;
}
@Dim, You have to use like below code(Using that you are to achieve what you want):
public static void isFencesActive(final Context context) {
Awareness.getFenceClient(context).queryFences(FenceQueryRequest.forFences(Arrays.asList(Constans.DETECTION_FENCE_DRIVING, Constans.DETECTION_FENCE_WALKING)))
.addOnSuccessListener(new OnSuccessListener<FenceQueryResponse>() {
@Override
public void onSuccess(FenceQueryResponse fenceQueryResponse) {
FenceStateMap map = fenceQueryResponse.getFenceStateMap();
if(!map.getFenceKeys().isEmpty()){
//Perform your operation here is the better way
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "Failed: " + e);
}
});
}
After some consideration I understood that is asynchronous task and that is why I receive false. What I did is created callbacks that send this info to my activity.
This may help you , Please declare boolean isActive;
variable globally.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.