Android Phone Alarm Clock Service alarmmanagerk development case _android

Source: Internet
Author: User

Alarmmanager is usually used to develop the phone alarm clock, and it is a global timer that can start other components at a specified time or a specified period (including activity,service,broadcastreceiver)

Get Alarmmanager object:

Getsystemservice (Service.alarm_service)

Call the way to set the timer to start the specified component:

The set (int type,long triggerattime,pendingintent operation) setting starts the component specified by the operation parameter at triggerattime time.

"The first parameter specifies the type of the timer service, and its value enables:
Elapsed_realtime: Specifies to start the operation to the appropriate component after a certain amount of time has passed since the current start time.
Elapsed_realtime_wakeup: Specifies the component that initiates the operation formation after a certain period of time, even if the system shutdown performs the corresponding component of the operation.
RTC: Specifies the component that initiates the operation when the system calls the System.currenttimemillls () method returns a value equal to Triggerattime.
Rtc_wakeup: Specifies the component that initiates the operation when the system calls the System.currenttimemillls () method returns a value equal to triggerattime, even if the system shutdown executes the component corresponding to the operation. ”

setinexactrepeationg (int type,long triggerattime,long interval,pendingintent operation): set up an inexact recurring task.

setrepeating (int type,long triggerattime,long interval,pendingintent operation): set up a recurring scheduled task.

cancle (pendingintent operation) cancel Alarmmanger timed service

Example:

Core code:

public class Mainactivity extends activity {private Alarmmanager Alarmmanager;
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.activity_main);
 Gets the Alarmmanager object alarmmanager= (Alarmmanager) Getsystemservice (Alarm_service);
  public void btn (view view) {final Calendar c = calendar.getinstance (); Timepickerdialog dialog = new Timepickerdialog (This, new Ontimesetlistener () {//Latency intent (that is, the intention of the alarm clock) pendingintent PaIn Tent = Pendingintent.getactivity (Mainactivity.this, 1, New Intent (Mainactivity.this,secondactivity.class), Pen
     Dingintent.flag_update_current); @Override public void Ontimeset (timepicker view, int hourofday, int minute) {//Set the time of your choice C.set (Calendar.
      Hour_of_day, Hourofday);
      C.set (Calendar.minute, MINUTE); The first parameter specifies the type of alarm service//second parameter specifies the time of the alarm interval//The third parameter refers to the delay intention of the jump alarm Alarmmanager.set (Alarmmanager.rtc_wakeup, C.gettimei Nmillis (),Paintent);
  },///Alarm's initial value (time and division) C.get (Calendar.hour_of_day), C.get (Calendar.minute), true);
 Dialog.show ();

 }
}

Layout:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"
 xmlns:tools= "http:// Schemas.android.com/tools "
 android:layout_width=" match_parent "
 android:layout_height=" Match_parent "
 android:paddingbottom= "@dimen/activity_vertical_margin"
 android:paddingleft= "@dimen/activity_ Horizontal_margin "
 android:paddingright=" @dimen/activity_horizontal_margin "
 android:paddingtop=" @dimen /activity_vertical_margin "
 tools:context=" com.example.alarmmanager.MainActivity ">

 <button
  android:onclick= "BTN"
  android:layout_width= "wrap_content"
  android:layout_height= "Wrap_content
  " android:text= "@string/btntext"/>

</RelativeLayout>

This is the entire content of this article, I hope to learn more about Android software programming help.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.