how to initialize final variable boolean array in method void










0















my application will check whether the play protect settings are activated or not.



public static boolean checkGooglePlayProtectSettings(Context context)
final boolean enabled = new booleanfalse;
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?










share|improve this question


























    0















    my application will check whether the play protect settings are activated or not.



    public static boolean checkGooglePlayProtectSettings(Context context)
    final boolean enabled = new booleanfalse;
    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?










    share|improve this question
























      0












      0








      0








      my application will check whether the play protect settings are activated or not.



      public static boolean checkGooglePlayProtectSettings(Context context)
      final boolean enabled = new booleanfalse;
      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?










      share|improve this question














      my application will check whether the play protect settings are activated or not.



      public static boolean checkGooglePlayProtectSettings(Context context)
      final boolean enabled = new booleanfalse;
      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?







      android android-studio boolean






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 4:20









      Robby RamadhanRobby Ramadhan

      1




      1






















          2 Answers
          2






          active

          oldest

          votes


















          0














          The isVerifyAppsEnabled() method is asynchronous, it calls the OnCompleteListener's onComplete method to notify when the result is ready.



          It's likely that this block is executed before the OnCompleteListener is fired, so the enabled[0] value remains the same at this point.



          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 (highly uncrecommended) perform a blocking wait until the Task returned by isVerifyAppsEnabled is completed.



          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()






          share|improve this answer






























            -1














            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



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



            2 using interface callback



             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.






            share|improve this answer

























            • That won't work due to asynchronous nature of the isVerifyAppsEnabled() method. And using Strings and StringBuilders instead of boolean values is a bad practice, it creates a lot of unnecessary objects, performs a lot of unneeded operations, lacks type safety and compile checks and it shouldn't be done at all. There is, in fact a term for this - Stringly Typed.

              – Mikhail Olshanski
              Nov 15 '18 at 5:30












            • @MikhailOlshanski yes, you are right, I modify answer, thank you!

              – navylover
              Nov 15 '18 at 5:38










            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312392%2fhow-to-initialize-final-variable-boolean-array-in-method-void%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            The isVerifyAppsEnabled() method is asynchronous, it calls the OnCompleteListener's onComplete method to notify when the result is ready.



            It's likely that this block is executed before the OnCompleteListener is fired, so the enabled[0] value remains the same at this point.



            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 (highly uncrecommended) perform a blocking wait until the Task returned by isVerifyAppsEnabled is completed.



            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()






            share|improve this answer



























              0














              The isVerifyAppsEnabled() method is asynchronous, it calls the OnCompleteListener's onComplete method to notify when the result is ready.



              It's likely that this block is executed before the OnCompleteListener is fired, so the enabled[0] value remains the same at this point.



              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 (highly uncrecommended) perform a blocking wait until the Task returned by isVerifyAppsEnabled is completed.



              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()






              share|improve this answer

























                0












                0








                0







                The isVerifyAppsEnabled() method is asynchronous, it calls the OnCompleteListener's onComplete method to notify when the result is ready.



                It's likely that this block is executed before the OnCompleteListener is fired, so the enabled[0] value remains the same at this point.



                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 (highly uncrecommended) perform a blocking wait until the Task returned by isVerifyAppsEnabled is completed.



                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()






                share|improve this answer













                The isVerifyAppsEnabled() method is asynchronous, it calls the OnCompleteListener's onComplete method to notify when the result is ready.



                It's likely that this block is executed before the OnCompleteListener is fired, so the enabled[0] value remains the same at this point.



                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 (highly uncrecommended) perform a blocking wait until the Task returned by isVerifyAppsEnabled is completed.



                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()







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 15 '18 at 5:17









                Mikhail OlshanskiMikhail Olshanski

                42349




                42349























                    -1














                    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



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



                    2 using interface callback



                     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.






                    share|improve this answer

























                    • That won't work due to asynchronous nature of the isVerifyAppsEnabled() method. And using Strings and StringBuilders instead of boolean values is a bad practice, it creates a lot of unnecessary objects, performs a lot of unneeded operations, lacks type safety and compile checks and it shouldn't be done at all. There is, in fact a term for this - Stringly Typed.

                      – Mikhail Olshanski
                      Nov 15 '18 at 5:30












                    • @MikhailOlshanski yes, you are right, I modify answer, thank you!

                      – navylover
                      Nov 15 '18 at 5:38















                    -1














                    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



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



                    2 using interface callback



                     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.






                    share|improve this answer

























                    • That won't work due to asynchronous nature of the isVerifyAppsEnabled() method. And using Strings and StringBuilders instead of boolean values is a bad practice, it creates a lot of unnecessary objects, performs a lot of unneeded operations, lacks type safety and compile checks and it shouldn't be done at all. There is, in fact a term for this - Stringly Typed.

                      – Mikhail Olshanski
                      Nov 15 '18 at 5:30












                    • @MikhailOlshanski yes, you are right, I modify answer, thank you!

                      – navylover
                      Nov 15 '18 at 5:38













                    -1












                    -1








                    -1







                    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



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



                    2 using interface callback



                     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.






                    share|improve this answer















                    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



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



                    2 using interface callback



                     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.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 15 '18 at 5:38

























                    answered Nov 15 '18 at 5:17









                    navylovernavylover

                    3,49031119




                    3,49031119












                    • That won't work due to asynchronous nature of the isVerifyAppsEnabled() method. And using Strings and StringBuilders instead of boolean values is a bad practice, it creates a lot of unnecessary objects, performs a lot of unneeded operations, lacks type safety and compile checks and it shouldn't be done at all. There is, in fact a term for this - Stringly Typed.

                      – Mikhail Olshanski
                      Nov 15 '18 at 5:30












                    • @MikhailOlshanski yes, you are right, I modify answer, thank you!

                      – navylover
                      Nov 15 '18 at 5:38

















                    • That won't work due to asynchronous nature of the isVerifyAppsEnabled() method. And using Strings and StringBuilders instead of boolean values is a bad practice, it creates a lot of unnecessary objects, performs a lot of unneeded operations, lacks type safety and compile checks and it shouldn't be done at all. There is, in fact a term for this - Stringly Typed.

                      – Mikhail Olshanski
                      Nov 15 '18 at 5:30












                    • @MikhailOlshanski yes, you are right, I modify answer, thank you!

                      – navylover
                      Nov 15 '18 at 5:38
















                    That won't work due to asynchronous nature of the isVerifyAppsEnabled() method. And using Strings and StringBuilders instead of boolean values is a bad practice, it creates a lot of unnecessary objects, performs a lot of unneeded operations, lacks type safety and compile checks and it shouldn't be done at all. There is, in fact a term for this - Stringly Typed.

                    – Mikhail Olshanski
                    Nov 15 '18 at 5:30






                    That won't work due to asynchronous nature of the isVerifyAppsEnabled() method. And using Strings and StringBuilders instead of boolean values is a bad practice, it creates a lot of unnecessary objects, performs a lot of unneeded operations, lacks type safety and compile checks and it shouldn't be done at all. There is, in fact a term for this - Stringly Typed.

                    – Mikhail Olshanski
                    Nov 15 '18 at 5:30














                    @MikhailOlshanski yes, you are right, I modify answer, thank you!

                    – navylover
                    Nov 15 '18 at 5:38





                    @MikhailOlshanski yes, you are right, I modify answer, thank you!

                    – navylover
                    Nov 15 '18 at 5:38

















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312392%2fhow-to-initialize-final-variable-boolean-array-in-method-void%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    ReactJS Fetched API data displays live - need Data displayed static

                    Evgeni Malkin