Example of AlarmManager usage in Android (updated continuously, changed), alarmmanager
Nowadays, mobile phones generally have an alarm function. If Android is used to implement an alarm, you can use AtarmManager. AtarmManager provides a system-level prompt service that allows you to execute a service at a certain time in the future. Generally, the AlarmManager object is not directly instantiated, but obtained through the Context. getsystemservice (Context. ALARM_SERVICE) method.
The following shows how to use the TimePickerDialog and AlarmManager to set an alert clock that can be set at any time and can be repeated. The following is an example (an alert is triggered at every night ):
Activity_main.xml layout file code in layout:
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout 3 xmlns: android = "http://schemas.android.com/apk/res/android" 4 xmlns: tools = "http://schemas.android.com/tools" 5 android: id = "@ + id/activity_main" 6 android: layout_width = "match_parent" 7 android: layout_height = "match_parent" 8 android: orientation = "vertical" 9 tools: context = "com. example. administrator. alarmdemo. mainActivity "> 10 <TextView11 android: id =" @ + id/time_ TV "12 android: layout_width =" match_parent "13 android: layout_height =" wrap_content "14 android: hint = "Please set the alert time" 15 android: clickable = "true" 16 android: onClick = "setTime" 17 android: gravity = "center"/> 18 <TextView19 android: id = "@ + id/time_tv2" 20 android: layout_width = "match_parent" 21 android: layout_height = "wrap_content" 22 android: hint = "Please set the bell interval" 23 android: clickable = "true" 24 android: onClick = "setIntervalTime" 25 android: gravity = "center"/> 26 <Button27 android: layout_width = "match_parent" 28 android: layout_height = "wrap_content" 29 android: onClick = "open" 30 android: text = "enable alarm"/> 31 <Button32 android: layout_width = "match_parent" 33 android: layout_height = "wrap_content" 34 android: onClick = "stop" 35 android: text = "end alarm"/> 36 </LinearLayout>
AndroidMainfest. xml configuration file (add custom duplicate alert ER ):
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.administrator.alarmdemo"> 4 <application 5 android:allowBackup="true" 6 android:icon="@mipmap/ic_launcher" 7 android:label="@string/app_name" 8 android:supportsRtl="true" 9 android:theme="@style/AppTheme">10 <activity android:name=".MainActivity">11 <intent-filter>12 <action android:name="android.intent.action.MAIN" />13 14 <category android:name="android.intent.category.LAUNCHER" />15 </intent-filter>16 </activity>17 <receiver18 android:name=".RepeatAlarmReceiver"19 android:enabled="true"20 android:exported="true">21 <intent-filter>22 <action android:name="repeatAlarm" />23 </intent-filter>24 </receiver>25 <activity android:name=".Main2Activity"></activity>26 </application>27 28 </manifest>
RepeatAlarmReceiver. java broadcast receiving file:
1 import android. content. broadcastReceiver; 2 import android. content. context; 3 import android. content. intent; 4 import android. widget. toast; 5 6 public class RepeatAlarmReceiver extends BroadcastReceiver {7 public RepeatAlarmReceiver () {8} 9 @ Override10 public void onReceive (Context context, Intent intent) {11 Toast. makeText (context, "Repeat alarm", Toast. LENGTH_SHORT ). show (); 12} 13}MainActivity. java code implementation:
1 import android. app. alarmManager; 2 import android. app. pendingIntent; 3 import android. app. timePickerDialog; 4 import android. content. intent; 5 import android. provider. settings; 6 import android. support. v7.app. appCompatActivity; 7 import android. OS. bundle; 8 import android. view. view; 9 import android. widget. textView; 10 import android. widget. timePicker; 11 import java. util. calendar; 12 public class MainActivity extends AppCompatActivity {13 AlarmManager alarmManagerSet; 14 PendingIntent pendingIntentSet; 15 int myHour, myMinute; // declare the ringing time 16 int intervalHour, intervalMinute, intervalSecond; // declare the interval (Bell cycle) 17 Calendar; // declare the calendar 18 TextView time_ TV, time_tv2; 19 20 @ Override21 protected void onCreate (Bundle savedInstanceState) {22 super. onCreate (savedInstanceState); 23 setContentView (R. layout. activity_main); 24 alarmManagerSet = (AlarmManager) getSystemService (ALARM_SERVICE); 25 // set the declared time format 26 calendar = Calendar. getInstance (); 27 myHour = calendar. get (Calendar. HOUR_OF_DAY); 28 myMinute = calendar. get (Calendar. MINUTE); 29 intervalHour = calendar. get (Calendar. HOUR_OF_DAY); 30 intervalMinute = calendar. get (Calendar. MINUTE); 31 // get the TextView32 time_ TV = (TextView) findViewById (R. id. time_ TV); 33 time_tv2 = (TextView) findViewById (R. id. time_tv2); 34} 35 // use timePickerDialog to set the alert time 36 public void setTime (View view) {37 TimePickerDialog timePickerDialog = new TimePickerDialog (MainActivity. this, new TimePickerDialog. onTimeSetListener () {38 @ Override39 public void onTimeSet (TimePicker view, int hourOfDay, int minute) {40 myHour = hourOfDay; 41 myMinute = minute; 42 minutes (myHour + ": "+ myMinute); 43} 44}, myHour, myMinute, true); 45 timePickerDialog. show (); 46} 47 // use timePickerDialog to set the alert cycle (interval) 48 public void setIntervalTime (View view View) {49 TimePickerDialog timePickerDialog = new TimePickerDialog (MainActivity. this, new TimePickerDialog. onTimeSetListener () {50 @ Override51 public void onTimeSet (TimePicker view, int hourOfDay, int minute) {52 intervalHour = hourOfDay; 53 intervalMinute = minute; 54 bytes (intervalHour + ": "+ intervalMinute); 55} 56}, intervalHour, intervalMinute, true); 57 timePickerDialog. show (); 58} 59 public void open (View view) {60 // obtain the alert time and alert interval respectively (separated by a colon and stored in the array) 61 String [] triggerTime = time_ TV .getText (). toString (). split (":"); 62 String [] intervalTime = time_tv2.getText (). toString (). split (":"); 63 // obtain the corresponding hour and minute (the String needs to be converted to int) by using Calendar. 64 Calendar calendar1 = Calendar. getInstance (); 65 calendar1.set (Calendar. HOUR_OF_DAY, Integer. parseInt (triggerTime [0]); 66 calendar1.set (Calendar. MINUTE, Integer. parseInt (triggerTime [1]); 67 // obtain the millisecond value triggered by a click (that is, the alarm time) 68 long triggerAtMillis = calendar1.getTimeInMillis (); 69 // set pendinngIntent to accept custom alert broadcast 70 pendingIntentSet = PendingIntent. getBroadcast (this, 0, new Intent ("repeatAlarm"), 0); 71 // determine if the current system time is greater than the Set alert time, enable the alarm 72 if (System. currentTimeMillis ()> triggerAtMillis) {73 triggerAtMillis = triggerAtMillis + 24*60*60*1000; 74} 75 // use. setRepeating setting repeated alarm 76 alarmManagerSet. setRepeating (AlarmManager. RTC_WAKEUP, triggerAtMillis, intervalMillis, pendingIntentSet); 77} 78 public void stop (View v) {79 alarmManagerSet. cancel (pendingIntentSet); 80} 81}