In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "Android development how to achieve the wake-up function after restart". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to realize the wake-up function after restart in Android development.
After the device is rebooted, applications that continue to run usually need to be restarted. By listening on the broadcast intent with the BOOT_COMPLETED operation, you can know whether the device has finished booting. As soon as you turn on the device, the system will send a BOOT_COMPLETEDbroadcast intent. To listen to it, create and register a standalone broadcast receiver.
Standalone receiver is a broadcast receiver declared in the manifest configuration file. Standalone receiver can be activated even if the application process is dead. (you'll also learn about dynamic receiver that can be bound to the lifecycle of fragment or activity later.)
Like services and activity, broadcast receiver must be registered in the system before it can function. If you don't register, the system doesn't know where to send intent. Naturally, broadcast receiver's onReceive (...) Method will not get the scheduled call.
To register a broadcast receiver, first create it. Create a new StartupReceiver class that inherits the android.content.BroadcastReceiver class, as shown below.
First broadcast receiver (StartupReceiver.java)
Public class StartupReceiver extends BroadcastReceiver {
Private static final String TAG = "StartupReceiver"
@ Override
Public void onReceive (Context context, Intent intent) {
Log.i (TAG, "Received broadcast intent:" + intent.getAction ())
}
}
Like services and activity, broadcast receiver is the component that receives intent. When an intent is sent to StartupReceiver, its onReceive (...) Method will be called. Open the AndroidManifest.xml configuration file and register on StartupReceiver according to the following code.
Add receiver (AndroidManifest.xml) to the manifest file
The standalone receiver that registers in response to the implicit intent is similar to the registration service or activity. We use the receiver tag and include the corresponding intent-filter in it. StartupReceiver listens for BOOT_COMPLETED operations, which also require configuration permissions. Therefore, you also need to add a corresponding uses-permission tag.
After completing the declaration in the configuration file, even if the application is not running, as long as a matching broadcast intent is sent, the broadcast receiver will wake up and receive it. The onReceive (Context, Intent) method of intent,broadcast receiver starts running as soon as it is received, and is then destroyed (as shown in the following figure).
After the device is rebooted, StartupReceiver's onReceive (...) Will the method be called? Let's verify it now. First, run the updated PhotoGallery application.
Then, turn off the device. If it is a physical device, press the power button directly to turn it off. If it is a simulator, the easiest way is to exit the simulator application directly.
Turn on the device. If it is a physical device, press the power button directly to turn it on. If it is an emulator, either rerun the application or use AVD Manager to start the application, but make sure you are using the emulator you just turned off.
Now, select the Tools → Android → Android Device Monitor menu item to open Android Device Monitor. (before KitKat, Android Device Monitor was often called Dalvik Debug Monitor Server or DDMS. Dalvik is a run-time system on Android. Since KitKat, Google has introduced ART (Android Runtime). When you get to Lollipop, only ART is available. Although the name has been changed to Android Device Monitor, the old name is still heard from time to time.)
Click on the device in the Devices option page of Android Device Monitor. (if you cannot see the list of devices, try unplugging the USB device or restarting the emulator.)
In the Android Device Monitor window, search for the LogCat output with the Received broadcast intent keyword (as shown in the following figure).
You can see the log indicating that receiver is running in LogCat. However, if you view the device on the device tab, you may not see any PhotoGallery processes. This is because the process dies immediately after running broadcast receiver.
Thank you for your reading. the above is the content of "how to realize the wake-up function after restart in Android development". After the study of this article, I believe you have a deeper understanding of how to realize the wake-up function after restart in Android development, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 274
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.