Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!
Deniel Kerr
Founder. Opencart
Top Partners
Recently, I was working on an Android app project which locks the phone until the user fills up a form and also notify the server that the device has been switched off. So, to achieve this I need to perform some task on the device boot up and shut down and to do so, my application should be listening that the device is started or switched off and do some tasks accordingly.
Android has provided a broadcast “android.intent.action.BOOT_COMPLETED” which will be fired when the device gets booted up and “android.intent.action.ACTION_SHUTDOWN” which will be fired on device shut down.
Let us look into the codes and understand how we can use these broadcasts and perform any task as per our requirement.
You need to create a broadcast listener which will be listening to the BOOT_COMPLETED and ACTION_SHUTDOWN broadcast. The below-provided codes suggest how to do that.
So, you can see in the above code segment that I have created a Broadcast receiver which checks the broadcast action and performs the tasks accordingly. You can add as many checks as you want and perform different tasks on different actions.
Now that we have created the broadcast listener, we would have to register it to listen to the broadcast fired by the system and perform a task on device boot up. We will do that in our AndroidManifest.xml file.
i) We will add the permission so that our application will be allowed to listen to these broadcasts.
ii) Add the broadcast listener using the receiver tag inside the application tag.
You will notice that we have added some other broadcasts as well. This receiver will be triggered on all of this broadcasts.
After this, you will be able to receive the broadcasts in your app. But there are a few points that need to carefully note.
Hope this has helped you in getting the idea of what exactly happening.
That’s all for this blog. Thank you very much. This is Vedesh Kumar signing off.
Your email address will not be published. Required fields are marked*
Name*
Email*
Save my name email and website in this browser for the next time I comment.
We use cookies to personalize your experience. By continuing to visit this website you agree to our use of cookies. Learn more about privacy policy
Name
Email
Subject
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.