简体   繁体   English

内部类内部的变量访问

[英]Variable access from within inner class

I created a function for checking fence state in Awareness API. 我在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): @Dim,您必须使用如下代码(使用您要实现的功能):

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; 这可能对您有帮助,请声明boolean isActive; variable globally. 全局变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM