Android Repeat alarm (daily) implementation

Source: Internet
Author: User
Tags locale

Mainactivity as follows:

Package Cc.cc;import Java.util.calendar;import Java.util.locale;import android.os.bundle;import Android.app.activity;import Android.app.alarmmanager;import Android.app.pendingintent;import Android.content.context;import android.content.intent;/** * Demo Description: * Recurring ALARM Implementation * * NOTE: * 1 because we are going to implement the two alarms every morning eight o'clock and eight o'clock in the evening, they are duplicated . * So it's easy to make mistakes in Pendingintent.getbroadcast (). The alarm clock that causes the eight o'clock * is invalid. The main reason is because of the fourth parameter of the method. * Workaround: Set two action for Broadcastreceiver, and use different action when generating intent *, so that two alarms will work. * If there is a better way, please leave a message to say, thank you. * 2 in the Calendar.set (Calendar.hour_of_day, 8) The second parameter of the method * denotes hours, please fill in the 24-hour format. What time does the device take? * Standard (12 hours or 24 hours) * Reference: * 1 htt p://blog.csdn.net/hudashi/article/details/7060837 * 2 http://blog.csdn.net/wdaming1986/article/details/6745655 * 3 http://blog.csdn.net/z1074971432/article/details/17532139 * Thank you very much * */public class Mainactivity extends Ac tivity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.main); sendalarmeverYday1 (this); SendAlarmEveryday2 (this);} private void SendAlarmEveryday1 (context context) {Alarmmanager Alarmmanager = (alarmmanager) getsystemservice ( Context.alarm_service);    Calendar Calendar =calendar.getinstance (Locale.getdefault ());    Calendar.settimeinmillis (System.currenttimemillis ());    Calendar.set (Calendar.hour_of_day, 8);    Calendar.set (Calendar.minute, 30);    Calendar.set (Calendar.second, 0);    Calendar.set (Calendar.millisecond, 0);    Intent Intent = new Intent ();    Intent.setaction ("Testalarm0");    Pendingintent pendingintent=pendingintent.getbroadcast (context,0, intent,pendingintent.flag_cancel_current); Alarmmanager.setrepeating (Alarmmanager.rtc_wakeup,calendar.gettimeinmillis (), Alarmmanager.interval_day, pendingintent);} private void SendAlarmEveryday2 (context context) {Alarmmanager Alarmmanager = (alarmmanager) getsystemservice ( Context.alarm_service);    Calendar Calendar =calendar.getinstance (Locale.getdefault ()); Calendar.settimeinmillis (System.currenttimemillis());    Calendar.set (Calendar.hour_of_day, 20);    Calendar.set (Calendar.minute, 30);    Calendar.set (Calendar.second, 0);    Calendar.set (Calendar.millisecond, 0);    Intent Intent = new Intent ();    Intent.setaction ("Testalarm1");    Pendingintent pendingintent=pendingintent.getbroadcast (context,0, intent,pendingintent.flag_cancel_current); Alarmmanager.setrepeating (Alarmmanager.rtc_wakeup,calendar.gettimeinmillis (), Alarmmanager.interval_day, pendingintent);}}

Alarmbroadcastreceiver as follows:

Package Cc.cc;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;public class Alarmbroadcastreceiver extends Broadcastreceiver {@Overridepublic void OnReceive ( Context context, Intent Intent) {if ("Testalarm0". Equals (Intent.getaction ())) {System.out.println ("First alarm Clock");} if ("Testalarm1". Equals (Intent.getaction ())) {System.out.println ("second Alarm");}}}

Androidmanifest.xml as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" cc.cc "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:mi nsdkversion= "android:targetsdkversion="/> <application android:allowbackup= "true" an        droid:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme" >            <activity android:name= "cc.cc.MainActivity" android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.MAIN"/> <categor                Y android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <receiver android:name= "Cc.cc.AlarmBroadcastReceiver" > <intent-filter > <  Action android:name= "Testalarm0"/>              <action android:name= "Testalarm1"/> </intent-filter> </receiver> </application> </manifest>

Main.xml as follows:

<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=". Mainactivity ">    <textview        android:layout_width=" wrap_content "        android:layout_height=" Wrap_ Content "        android:text=" @string/hello_world "/></relativelayout>


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.