How to get broadcast ACTION_POWER_CONNECTED on Android 8












1















I am trying to make an app that shows some information when you connect your phone to a charger during a specific period of the day.



Before Android Oreo this was easy, just add an Broadcast receiver and add android.intent.action.ACTION_POWER_CONNECTED to the intentfilter in the manifest.



Now I am struggling to find a working solution to create the same functionality.
I thought about periodically checking the device charging state but this feels wrong and it won't trigger immediately on connecting the device to a charger.



Registering the broadcast receiver from my application did not work either, when the application is closed the broadcast isn't received anymore.



Is there a battery friendly way to trigger an action when an Android device is connected to a charger in Android Oreo?










share|improve this question





























    1















    I am trying to make an app that shows some information when you connect your phone to a charger during a specific period of the day.



    Before Android Oreo this was easy, just add an Broadcast receiver and add android.intent.action.ACTION_POWER_CONNECTED to the intentfilter in the manifest.



    Now I am struggling to find a working solution to create the same functionality.
    I thought about periodically checking the device charging state but this feels wrong and it won't trigger immediately on connecting the device to a charger.



    Registering the broadcast receiver from my application did not work either, when the application is closed the broadcast isn't received anymore.



    Is there a battery friendly way to trigger an action when an Android device is connected to a charger in Android Oreo?










    share|improve this question



























      1












      1








      1








      I am trying to make an app that shows some information when you connect your phone to a charger during a specific period of the day.



      Before Android Oreo this was easy, just add an Broadcast receiver and add android.intent.action.ACTION_POWER_CONNECTED to the intentfilter in the manifest.



      Now I am struggling to find a working solution to create the same functionality.
      I thought about periodically checking the device charging state but this feels wrong and it won't trigger immediately on connecting the device to a charger.



      Registering the broadcast receiver from my application did not work either, when the application is closed the broadcast isn't received anymore.



      Is there a battery friendly way to trigger an action when an Android device is connected to a charger in Android Oreo?










      share|improve this question
















      I am trying to make an app that shows some information when you connect your phone to a charger during a specific period of the day.



      Before Android Oreo this was easy, just add an Broadcast receiver and add android.intent.action.ACTION_POWER_CONNECTED to the intentfilter in the manifest.



      Now I am struggling to find a working solution to create the same functionality.
      I thought about periodically checking the device charging state but this feels wrong and it won't trigger immediately on connecting the device to a charger.



      Registering the broadcast receiver from my application did not work either, when the application is closed the broadcast isn't received anymore.



      Is there a battery friendly way to trigger an action when an Android device is connected to a charger in Android Oreo?







      android broadcastreceiver android-8.0-oreo






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 17:46









      Matt Ke

      1,3864823




      1,3864823










      asked Nov 22 '18 at 21:05









      1-0-1 Development1-0-1 Development

      84




      84
























          1 Answer
          1






          active

          oldest

          votes


















          2














          There are broadcasts which are exempted from the background execution limitations and for which broadcast receivers can still be registered in the manifest.



          You could use the ACTION_BOOT_COMPLETED broadcast to start a service which registers a receiver for your ACTION_POWER_CONNECTED broadcast at runtime. (The service must be a foreground service. Otherwise, it may be destroyed.)



          A other solution would be to use JobScheduler to create a job which requires charging. Then you don't need the foreground service.






          share|improve this answer


























          • I used the foreground service for all android versions beyond N.The service registers a BroadcastReceiver for ACTION_POWER_CONNECTED. Next I used a BOOT_COMPLETE BroadcastReceiver to start the foreground service. For earlier Android versions I have used the standard method of using a BroadcastReceiver and adding ACTION_POWER_CONNECTED to the manifest.

            – 1-0-1 Development
            Nov 25 '18 at 9:01













          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%2f53437978%2fhow-to-get-broadcast-action-power-connected-on-android-8%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          There are broadcasts which are exempted from the background execution limitations and for which broadcast receivers can still be registered in the manifest.



          You could use the ACTION_BOOT_COMPLETED broadcast to start a service which registers a receiver for your ACTION_POWER_CONNECTED broadcast at runtime. (The service must be a foreground service. Otherwise, it may be destroyed.)



          A other solution would be to use JobScheduler to create a job which requires charging. Then you don't need the foreground service.






          share|improve this answer


























          • I used the foreground service for all android versions beyond N.The service registers a BroadcastReceiver for ACTION_POWER_CONNECTED. Next I used a BOOT_COMPLETE BroadcastReceiver to start the foreground service. For earlier Android versions I have used the standard method of using a BroadcastReceiver and adding ACTION_POWER_CONNECTED to the manifest.

            – 1-0-1 Development
            Nov 25 '18 at 9:01


















          2














          There are broadcasts which are exempted from the background execution limitations and for which broadcast receivers can still be registered in the manifest.



          You could use the ACTION_BOOT_COMPLETED broadcast to start a service which registers a receiver for your ACTION_POWER_CONNECTED broadcast at runtime. (The service must be a foreground service. Otherwise, it may be destroyed.)



          A other solution would be to use JobScheduler to create a job which requires charging. Then you don't need the foreground service.






          share|improve this answer


























          • I used the foreground service for all android versions beyond N.The service registers a BroadcastReceiver for ACTION_POWER_CONNECTED. Next I used a BOOT_COMPLETE BroadcastReceiver to start the foreground service. For earlier Android versions I have used the standard method of using a BroadcastReceiver and adding ACTION_POWER_CONNECTED to the manifest.

            – 1-0-1 Development
            Nov 25 '18 at 9:01
















          2












          2








          2







          There are broadcasts which are exempted from the background execution limitations and for which broadcast receivers can still be registered in the manifest.



          You could use the ACTION_BOOT_COMPLETED broadcast to start a service which registers a receiver for your ACTION_POWER_CONNECTED broadcast at runtime. (The service must be a foreground service. Otherwise, it may be destroyed.)



          A other solution would be to use JobScheduler to create a job which requires charging. Then you don't need the foreground service.






          share|improve this answer















          There are broadcasts which are exempted from the background execution limitations and for which broadcast receivers can still be registered in the manifest.



          You could use the ACTION_BOOT_COMPLETED broadcast to start a service which registers a receiver for your ACTION_POWER_CONNECTED broadcast at runtime. (The service must be a foreground service. Otherwise, it may be destroyed.)



          A other solution would be to use JobScheduler to create a job which requires charging. Then you don't need the foreground service.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 '18 at 22:38

























          answered Nov 22 '18 at 22:16









          Matt KeMatt Ke

          1,3864823




          1,3864823













          • I used the foreground service for all android versions beyond N.The service registers a BroadcastReceiver for ACTION_POWER_CONNECTED. Next I used a BOOT_COMPLETE BroadcastReceiver to start the foreground service. For earlier Android versions I have used the standard method of using a BroadcastReceiver and adding ACTION_POWER_CONNECTED to the manifest.

            – 1-0-1 Development
            Nov 25 '18 at 9:01





















          • I used the foreground service for all android versions beyond N.The service registers a BroadcastReceiver for ACTION_POWER_CONNECTED. Next I used a BOOT_COMPLETE BroadcastReceiver to start the foreground service. For earlier Android versions I have used the standard method of using a BroadcastReceiver and adding ACTION_POWER_CONNECTED to the manifest.

            – 1-0-1 Development
            Nov 25 '18 at 9:01



















          I used the foreground service for all android versions beyond N.The service registers a BroadcastReceiver for ACTION_POWER_CONNECTED. Next I used a BOOT_COMPLETE BroadcastReceiver to start the foreground service. For earlier Android versions I have used the standard method of using a BroadcastReceiver and adding ACTION_POWER_CONNECTED to the manifest.

          – 1-0-1 Development
          Nov 25 '18 at 9:01







          I used the foreground service for all android versions beyond N.The service registers a BroadcastReceiver for ACTION_POWER_CONNECTED. Next I used a BOOT_COMPLETE BroadcastReceiver to start the foreground service. For earlier Android versions I have used the standard method of using a BroadcastReceiver and adding ACTION_POWER_CONNECTED to the manifest.

          – 1-0-1 Development
          Nov 25 '18 at 9:01






















          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%2f53437978%2fhow-to-get-broadcast-action-power-connected-on-android-8%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







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()