How to reliably get the screen unlock event for API Level >= 26?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







3















As per official documentation,the BroadcastReceiver for screen lock/unlock events can only be registered dynamically (by calling registerReceiver from within the Activity) starting from Android O (API 26). Registering these events in the AndroidManifest.xml no longer works hence an application is no longer capable of receiving these events (ACTION_SCREEN_ON/ ACTION_USER_PRESENT) when the app is not running (has been previously killed by Android or force closed by user).



Same is also mentioned in the post :
Android - detect phone unlock event, not screen on



Is there any other alternate reliable and possible way to get screen lock/unlock event ?



Tried implicit broadcast for receiving screen lock/unlock event by registering ACTION_USER_PRESENT in AndroidManifest .xml file of the app. But as per doc, implcit broadcast apart from some exceptions won't work. You have to do it using dynamic registration by registerReceiver in activity class. So, if the app is killed by Android OS or maybe force stopped by the user, screen lock/unlock broadcast is not received by the app.



Activity class dynamic broadcast receiver



 registerReceiver(new PhoneUnlockedReceiver(), 
new IntentFilter("android.intent.action.USER_PRESENT"));

public class PhoneUnlockedReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(
Context.KEYGUARD_SERVICE);
if (keyguardManager.isKeyguardSecure()) {

//phone was unlocked, add handling here

}
}
}









share|improve this question































    3















    As per official documentation,the BroadcastReceiver for screen lock/unlock events can only be registered dynamically (by calling registerReceiver from within the Activity) starting from Android O (API 26). Registering these events in the AndroidManifest.xml no longer works hence an application is no longer capable of receiving these events (ACTION_SCREEN_ON/ ACTION_USER_PRESENT) when the app is not running (has been previously killed by Android or force closed by user).



    Same is also mentioned in the post :
    Android - detect phone unlock event, not screen on



    Is there any other alternate reliable and possible way to get screen lock/unlock event ?



    Tried implicit broadcast for receiving screen lock/unlock event by registering ACTION_USER_PRESENT in AndroidManifest .xml file of the app. But as per doc, implcit broadcast apart from some exceptions won't work. You have to do it using dynamic registration by registerReceiver in activity class. So, if the app is killed by Android OS or maybe force stopped by the user, screen lock/unlock broadcast is not received by the app.



    Activity class dynamic broadcast receiver



     registerReceiver(new PhoneUnlockedReceiver(), 
    new IntentFilter("android.intent.action.USER_PRESENT"));

    public class PhoneUnlockedReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

    KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(
    Context.KEYGUARD_SERVICE);
    if (keyguardManager.isKeyguardSecure()) {

    //phone was unlocked, add handling here

    }
    }
    }









    share|improve this question



























      3












      3








      3








      As per official documentation,the BroadcastReceiver for screen lock/unlock events can only be registered dynamically (by calling registerReceiver from within the Activity) starting from Android O (API 26). Registering these events in the AndroidManifest.xml no longer works hence an application is no longer capable of receiving these events (ACTION_SCREEN_ON/ ACTION_USER_PRESENT) when the app is not running (has been previously killed by Android or force closed by user).



      Same is also mentioned in the post :
      Android - detect phone unlock event, not screen on



      Is there any other alternate reliable and possible way to get screen lock/unlock event ?



      Tried implicit broadcast for receiving screen lock/unlock event by registering ACTION_USER_PRESENT in AndroidManifest .xml file of the app. But as per doc, implcit broadcast apart from some exceptions won't work. You have to do it using dynamic registration by registerReceiver in activity class. So, if the app is killed by Android OS or maybe force stopped by the user, screen lock/unlock broadcast is not received by the app.



      Activity class dynamic broadcast receiver



       registerReceiver(new PhoneUnlockedReceiver(), 
      new IntentFilter("android.intent.action.USER_PRESENT"));

      public class PhoneUnlockedReceiver extends BroadcastReceiver {

      @Override
      public void onReceive(Context context, Intent intent) {

      KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(
      Context.KEYGUARD_SERVICE);
      if (keyguardManager.isKeyguardSecure()) {

      //phone was unlocked, add handling here

      }
      }
      }









      share|improve this question
















      As per official documentation,the BroadcastReceiver for screen lock/unlock events can only be registered dynamically (by calling registerReceiver from within the Activity) starting from Android O (API 26). Registering these events in the AndroidManifest.xml no longer works hence an application is no longer capable of receiving these events (ACTION_SCREEN_ON/ ACTION_USER_PRESENT) when the app is not running (has been previously killed by Android or force closed by user).



      Same is also mentioned in the post :
      Android - detect phone unlock event, not screen on



      Is there any other alternate reliable and possible way to get screen lock/unlock event ?



      Tried implicit broadcast for receiving screen lock/unlock event by registering ACTION_USER_PRESENT in AndroidManifest .xml file of the app. But as per doc, implcit broadcast apart from some exceptions won't work. You have to do it using dynamic registration by registerReceiver in activity class. So, if the app is killed by Android OS or maybe force stopped by the user, screen lock/unlock broadcast is not received by the app.



      Activity class dynamic broadcast receiver



       registerReceiver(new PhoneUnlockedReceiver(), 
      new IntentFilter("android.intent.action.USER_PRESENT"));

      public class PhoneUnlockedReceiver extends BroadcastReceiver {

      @Override
      public void onReceive(Context context, Intent intent) {

      KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(
      Context.KEYGUARD_SERVICE);
      if (keyguardManager.isKeyguardSecure()) {

      //phone was unlocked, add handling here

      }
      }
      }






      android android-api-levels






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 4 at 10:29









      Fantômas

      32.9k156491




      32.9k156491










      asked Jan 4 at 9:15









      Ameya721Ameya721

      214




      214
























          0






          active

          oldest

          votes












          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%2f54035979%2fhow-to-reliably-get-the-screen-unlock-event-for-api-level-26%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          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%2f54035979%2fhow-to-reliably-get-the-screen-unlock-event-for-api-level-26%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

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas