简体   繁体   English

如何在方法void中初始化最终变量布尔数组

[英]how to initialize final variable boolean array in method void

my application will check whether the play protect settings are activated or not.我的应用程序将检查播放保护设置是否已激活。

public static boolean checkGooglePlayProtectSettings(Context context){
    final boolean[] enabled = new boolean[]{false};
    try{
        SafetyNet.getClient(context).isVerifyAppsEnabled().addOnCompleteListener(new OnCompleteListener<SafetyNetApi.VerifyAppsUserResponse>() {

            @Override
                    public void onComplete(@NonNull Task<SafetyNetApi.VerifyAppsUserResponse> task) {
                        if(task.isSuccessful()){
                            SafetyNetApi.VerifyAppsUserResponse result = task.getResult();
                            if(result != null){
                                if(result.isVerifyAppsEnabled()){
                                    enabled[0] = true;
                                }else{
                                    enabled[0] = false;

                                }
                            }
                        }else{
                            Log.e("GoogleProtectPermission", "A general error occurred.");
                        }
                    }
                });
    }catch (Exception e){
        e.printStackTrace();
    }

    if (enabled[0]){
        Log.d("GoogleProtectPermission", "Is enabled");
    }else {
        Log.d("GoogleProtectPermission", "Is disabled");
    }
    return false;
}

in this case I tried the final results to log, but always not active in the log results, how to get good results to check whether play prtoect is active or not?在这种情况下,我尝试将最终结果记录下来,但在日志结果中始终不活跃,如何获得良好的结果来检查播放保护是否处于活动状态?

The isVerifyAppsEnabled() method is asynchronous, it calls the OnCompleteListener 's onComplete method to notify when the result is ready. isVerifyAppsEnabled()方法是异步的,它调用OnCompleteListeneronComplete方法来通知结果何时准备就绪。

It's likely that this block is executed before the OnCompleteListener is fired, so the enabled[0] value remains the same at this point.该块很可能在OnCompleteListener被触发之前执行,因此enabled[0]值此时保持不变。

if (enabled[0]) {
    Log.d("GoogleProtectPermission", "Is enabled");
} else {
    Log.d("GoogleProtectPermission", "Is disabled");
}

Since there's no synchronous method to get the result, you'll probably have to change your approach someway to take account for this asynchronicity and perform dependent operations when the callback results are ready or perform a blocking wait until the Task returned by isVerifyAppsEnabled is completed.由于没有同步方法来获取结果,您可能必须以某种方式更改您的方法以考虑这种异步性并在回调结果准备好时执行相关操作或执行阻塞等待直到isVerifyAppsEnabled返回的Task完成。

Also, disregarding the question, in your current code there seems to be little point in using boolean array instead of just a boolean for this, and this另外,不管这个问题,在您当前的代码中,使用布尔数组而不仅仅是一个布尔值似乎没有什么意义,而这个

if(result.isVerifyAppsEnabled()) {
    enabled[0] = true;
} else {
    enabled[0] = false;
}

can be simplified to this enabled[0] = result.isVerifyAppsEnabled()可以简化为这个enabled[0] = result.isVerifyAppsEnabled()

You could do like this:你可以这样做:

final StringBuilder sb = new StringBuilder();
...
if(result.isVerifyAppsEnabled()){
    sb.append("good");
}else{
    sb.append("bad");
}
...
if(sb.toString().equals("good")){
    Log.d("GoogleProtectPermission", "Is enabled");
}
if(sb.toString().equals("bad")){
    Log.d("GoogleProtectPermission", "Is disabled");
}

Edited: Yes, as comment points, My answer is wrong.编辑:是的,作为评论点,我的回答是错误的。 so there are two ways:所以有两种方法:

1 directly output log info 1 直接输出日志信息

   if(result.isVerifyAppsEnabled()){
       Log.d("GoogleProtectPermission", "Is enabled");
    }else{
        Log.d("GoogleProtectPermission", "Is disabled");
    }

2 using interface callback 2 使用接口回调

   public interface Callback {  //add an interface 
        void onResponse(String message);
    }

    final StringBuilder sb = new StringBuilder();
    ...
    if(result.isVerifyAppsEnabled()){
        sb.append("good");
    }else{
        sb.append("bad");
    }
    callback.onResponse(sb.toString()); //add this line

and subscribe the interface in some place.并在某个地方订阅该接口。

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

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