Android implements simple timing reminder function code based on broadcast event mechanism

This paper describes the simple timing reminder function code of Android based on broadcast event mechanism. Share with you for your reference, as follows:

1. Android broadcast event mechanism

Android broadcast event processing is similar to ordinary event processing. The difference is that the latter is triggered by the component behavior of clicking a button, while the former transmits information by building an intent object and using the sendbroadcast () method to initiate a system level event broadcast. The reception of broadcast events is realized by defining a class that inherits broadcast receiver. After inheriting this class, override its onReceive () method and respond to events in this method. Many standard broadcast actions are defined in the Android system to respond to system broadcast events. For example: action_ TIME_ Changed (triggered when the time changes). However, we can also define our own broadcast receiver to receive broadcast events.

2. Realize simple timing reminder function

It mainly includes three parts:

1) Timing - issue broadcast by defining activity 2) receive broadcast - receive broadcast by implementing broadcastreceiver 3) remind - and remind users through notification

Now let's implement these three parts:

2.1 how to time the broadcast?

Now mobile phones have the function of alarm clock. We can use the alarm clock function provided by the system to time, that is, send a broadcast. Specifically, it can be implemented with AlarmManager in Android development.

AlarmManager provides a system level prompt service that allows you to schedule a service to be executed at a certain time. The steps for using AlarmManager are as follows:

1) Obtain the AlarmManager instance: generally, the AlarmManager object is not instantiated directly, but obtained through the context. Getsystemservice (context. Alarm_service) method. 2) define a pendingintent to broadcast. 3) Call the relevant methods of AlarmManager to set timing, repeated reminder and other functions.

The detailed code is as follows (remindersetting. Java):

2.2 receiving broadcast

Create a new class that inherits broadcastreceiver and implements onreceive() method. When the broadcastreceiver receives the broadcast, it will execute the onReceive () method. Therefore, we add code to the onReceive () method to jump to the activity that displays the reminder information after receiving the broadcast. The specific code is as follows (myreceiver. Java):

Note: after creating the broadcastreceiver, you need to register in androidmanifest.xml:

2.3 reminder function

Create a new activity in which we remind users through the notification object of Android. We will add a prompt tone, a textview to display the prompt content and a button to cancel the prompt.

Among them, creating a notification mainly includes:

1) Obtain the system level service notificationmanager through context. Getsystemservice (notification_service). 2) Instantiate the notification object and set various properties we need, such as setting sound. 3) Call the notify() method of notificationmanager to display the notification

The detailed code is as follows: myalarm.java

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>