Use of Alarm in Android

Source: Internet
Author: User

UseAlarmManagerTo set your own alarm, among which the main classes we use are

 
 
  1. import android.app.AlarmManager;    
  2. import android.app.PendingIntent;    
  3. import android.app.TimePickerDialog;   

These three are the classes that we must use. Remember, can we use the wrong package? The most important thing is that everyone is easy to forget, we must add permissions so that we can achieve the desired effect smoothly. The Code is as follows:

Java code:

 
 
  1. Package eoe. demo;
  2. Import android. content. BroadcastReceiver;
  3. Import android. content. Context;
  4. Import android. content. Intent;
  5. Import android. widget. Toast;
  6. Public class AlamrReceiver extends BroadcastReceiver {
  7. @ Override
  8. Public void onReceive (Context context, Intent intent ){
  9. // TODO Auto-generated method stub
  10. Toast. makeText (context, "alarm time to", Toast. LENGTH_LONG). show ();
  11. }
  12. }

Java code:

 
 
  1. Package eoe. demo;
  2. Import java. util. Calendar;
  3. Import android. app. Activity;
  4. Import android. app. AlarmManager;
  5. Import android. app. PendingIntent;
  6. Import android. app. TimePickerDialog;
  7. Import android. content. Intent;
  8. Import android. OS. Bundle;
  9. Import android. view. View;
  10. Import android. widget. Button;
  11. Import android. widget. TextView;
  12. Import android. widget. TimePicker;
  13. Public class Test extends Activity {
  14. /** Called when the activity is first created .*/
  15. Private TextView TV = null;
  16. Private Button btn_set = null;
  17. Private Button btn_cel = null;
  18. Private Calendar c = null;
  19. @ Override
  20. Public void onCreate (Bundle savedInstanceState ){
  21. Super. onCreate (savedInstanceState );
  22. SetContentView (R. layout. main );
  23. TV = (TextView) this. findViewById (R. id. TextView );
  24. Btn_set = (Button) this. findViewById (R. id. Button01 );
  25. Btn_cel = (Button) this. findViewById (R. id. Button02 );
  26. C = Calendar. getInstance ();
  27. Btn_set.setOnClickListener (new Button. OnClickListener (){
  28. Public void onClick (View v ){
  29. // TODO Auto-generated method stub
  30. C. setTimeInMillis (System. currentTimeMillis ());
  31. Int hour = c. get (Calendar. HOUR_OF_DAY );
  32. Int minute = c. get (Calendar. MINUTE );
  33. New TimePickerDialog (Test. this, new TimePickerDialog. OnTimeSetListener (){
  34. Public void onTimeSet (TimePicker view, int hourOfDay, int minute ){
  35. // TODO Auto-generated method stub
  36. C. setTimeInMillis (System. currentTimeMillis ());
  37. C. set (Calendar. HOUR_OF_DAY, hourOfDay );
  38. C. set (Calendar. MINUTE, minute );
  39. C. set (Calendar. SECOND, 0 );
  40. C. set (Calendar. MILLISECOND, 0 );
  41. Intent intent = new Intent (Test. this, AlamrReceiver. class );
  42. PendingIntent pi = PendingIntent. getBroadcast (Test. this, 0, intent, 0 );
  43. AlarmManager am = (AlarmManager) getSystemService (Activity. ALARM_SERVICE );
  44. Am. set (AlarmManager. RTC_WAKEUP, c. getTimeInMillis (), pi); // set the alarm
  45. Am. setRepeating (AlarmManager. RTC_WAKEUP, c. getTimeInMillis (), (10*1000), pi); // repeat settings
  46. TV. setText ("the set alarm time is:" + hourOfDay + ":" + minute );
  47. }
  48. }, Hour, minute, true). show ();
  49. }
  50. });
  51. Btn_cel.setOnClickListener (new Button. OnClickListener (){
  52. Public void onClick (View v ){
  53. // TODO Auto-generated method stub
  54. Intent intent = new Intent (Test. this, AlamrReceiver. class );
  55. PendingIntent pi = PendingIntent. getBroadcast (Test. this, 0, intent, 0 );
  56. AlarmManager am = (AlarmManager) getSystemService (Activity. ALARM_SERVICE );
  57. Am. cancel (pi );
  58. TV. setText ("Cancel alarm ");
  59. }
  60. });
  61. }
  62. }

Java code:

 
 
  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
  3. Android: orientation = "vertical" android: layout_width = "fill_parent"
  4. Android: layout_height = "fill_parent">
  5. <TextView android: layout_width = "fill_parent"
  6. Android: id = "@ + id/TextView"
  7. Android: layout_height = "wrap_content" android: text = "@ string/hello"/>
  8. <Button android: text = "Set alert Clock" android: id = "@ + id/Button01"
  9. Android: layout_width = "wrap_content"
  10. Android: layout_height = "wrap_content">
  11. </Button>
  12. <Button android: text = "Cancel alarm" android: id = "@ + id/Button02"
  13. Android: layout_width = "wrap_content"
  14. Android: layout_height = "wrap_content">
  15. </Button>
  16. </LinearLayout>

Java code:

 
 
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  3. package="eoe.demo"   
  4. android:versionCode="1"   
  5. android:versionName="1.0">   
  6. <application android:icon="@drawable/icon" android:label="@string/app_name">   
  7. <activity android:name=".Test"   
  8. android:label="@string/app_name">   
  9. <intent-filter>   
  10. <action android:name="android.intent.action.MAIN" />   
  11. <category android:name="android.intent.category.LAUNCHER" />   
  12. </intent-filter>   
  13. </activity>   
  14. <receiver android:name=".AlamrReceiver" android:process=":remote"></receiver>   
  15. </application>   
  16. </manifest> 

Insert a new Activity in Android Development

Android multi-task multi-thread breakpoint download

Android development tour: Android Architecture

Common Android commands and simulator Parameters

Use Internet Data in Android applications

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.