Android alarm settings

Source: Internet
Author: User

The alarm clock is the most common in life. In Android, you can use AlarmManager to implement the alarm. The AlarmManager class is used to set the specified time to be completed at a specified time. AlarmManager will execute these events through the onReceive () method. Even if the system is in the STANDBY state, the operation will not be affected. You can use the Context. getSystemService method to obtain the service. There are many methods in AlarmManager, as follows:
 

Method Description
Cancel Cancel AlarmManager Service
Set Set the AlarmManager Service
SetInexactRepeating Set an inaccurate Period
SetRepeating Set exact cycle
SetTimeZone Set Time Zone
 
To implement an alert clock, you must first create a class inherited from BroadcastReceiver to implement the onReceive method to accept this Alarm service, and then call the Alarm component by establishing a connection between Intent and PendingIntent. Use TimerPickerDialog to set the alert time. When the time reaches the specified time, the oncycler method receives the Alarm service interface.
First, implement the alarmcycler class that accepts the Alarm service, and use the Toast class to prompt the user
 

[Java] public class AlarmReceiver extends BroadcastReceiver {
 
@ Override
Public void onReceive (Context arg0, Intent arg1 ){
// TODO Auto-generated method stub
Toast. makeText (arg0, "the alert time you set is reached", Toast. LENGTH_LONG). show ();
}
 
}
Public class AlarmReceiver extends BroadcastReceiver {

@ Override
Public void onReceive (Context arg0, Intent arg1 ){
// TODO Auto-generated method stub
Toast. makeText (arg0, "the alert time you set is reached", Toast. LENGTH_LONG). show ();
}

}
 

 


Because the BroadcastReceiver service is used, you must declare it in AndroidManifest. xml:

 


[Java] <author er
Android: name = ". AlarmReceiver"
Android: process = ": remote">
</Cycler>
Then, you need to set the alarm and cancel the alarm:
Package cn.edu. pku;
 
Import java. util. Calendar;
 
Import android. app. Activity;
Import android. app. AlarmManager;
Import android. app. PendingIntent;
Import android. app. TimePickerDialog;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Button;
Import android. widget. TextView;
Import android. widget. TimePicker;
 
Public class AlarmActivity extends Activity {
/** Called when the activity is first created .*/

Button mButton1;
Button mButton2;

TextView mTextView;

Calendar calendar;

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

Calendar = Calendar. getInstance ();

MTextView = (TextView) findViewById (R. id. TextView01 );
MButton1 = (Button) findViewById (R. id. Button01 );
MButton2 = (Button) findViewById (R. id. Button02 );

MButton1.setOnClickListener (new View. OnClickListener (){

Public void onClick (View v ){
// TODO Auto-generated method stub
Calendar. setTimeInMillis (System. currentTimeMillis ());
Int hour = calendar. get (Calendar. HOUR_OF_DAY );
Int minute = calendar. get (Calendar. MINUTE );
New TimePickerDialog (AlarmActivity. this, new TimePickerDialog. OnTimeSetListener (){

Public void onTimeSet (TimePicker view, int hourOfDay, int minute ){
// TODO Auto-generated method stub
Calendar. setTimeInMillis (System. currentTimeMillis ());
Calendar. set (Calendar. HOUR_OF_DAY, hourOfDay );
Calendar. set (Calendar. MINUTE, minute );
Calendar. set (Calendar. SECOND, 0 );
Calendar. set (Calendar. MILLISECOND, 0 );
Intent intent = new Intent (AlarmActivity. this, AlarmReceiver. class );
PendingIntent pendingIntent = PendingIntent. getBroadcast (AlarmActivity. this, 0, intent, 0 );
AlarmManager alarmManager = (AlarmManager) getSystemService (ALARM_SERVICE );
AlarmManager. set (AlarmManager. RTC_WAKEUP, calendar. getTimeInMillis (), pendingIntent );
AlarmManager. setRepeating (AlarmManager. RTC_WAKEUP, System. currentTimeMillis () + (10*1000 ),
(24x60x60x1000), pendingIntent );
String tmps = "set the alarm time to" + format (hourOfDay) + ":" + format (minute );
MTextView. setText (tmps );
}
}, Hour, minute, true). show ();
}
});

MButton2.setOnClickListener (new View. OnClickListener (){

Public void onClick (View v ){
// TODO Auto-generated method stub
Intent intent = new Intent (AlarmActivity. this, AlarmReceiver. class );
PendingIntent pendingIntent = PendingIntent. getBroadcast (AlarmActivity. this, 0, intent, 0 );
AlarmManager alarmManager = (AlarmManager) getSystemService (ALARM_SERVICE );
AlarmManager. cancel (pendingIntent );
MTextView. setText ("the alarm has been canceled! ");
}
});
}

Private String format (int time ){
String str = "" + time;
If (str. length () = 1 ){
Str = "0" + str;
}
Return str;
}
}
<Cycler
Android: name = ". AlarmReceiver"
Android: process = ": remote">
</Cycler>
Then, you need to set the alarm and cancel the alarm:
Package cn.edu. pku;

Import java. util. Calendar;

Import android. app. Activity;
Import android. app. AlarmManager;
Import android. app. PendingIntent;
Import android. app. TimePickerDialog;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Button;
Import android. widget. TextView;
Import android. widget. TimePicker;

Public class AlarmActivity extends Activity {
/** Called when the activity is first created .*/
 
Button mButton1;
Button mButton2;
 
TextView mTextView;
 
Calendar calendar;
 
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

Calendar = Calendar. getInstance ();

MTextView = (TextView) findViewById (R. id. TextView01 );
MButton1 = (Button) findViewById (R. id. Button01 );
MButton2 = (Button) findViewById (R. id. Button02 );

MButton1.setOnClickListener (new View. OnClickListener (){

Public void onClick (View v ){
// TODO Auto-generated method stub
Calendar. setTimeInMillis (System. currentTimeMillis ());
Int hour = calendar. get (Calendar. HOUR_OF_DAY );
Int minute = calendar. get (Calendar. MINUTE );
New TimePickerDialog (AlarmActivity. this, new TimePickerDialog. OnTimeSetListener (){

Public void onTimeSet (TimePicker view, int hourOfDay, int minute ){
// TODO Auto-generated method stub
Calendar. setTimeInMillis (System. currentTimeMillis ());
Calendar. set (Calendar. HOUR_OF_DAY, hourOfDay );
Calendar. set (Calendar. MINUTE, minute );
Calendar. set (Calendar. SECOND, 0 );
Calendar. set (Calendar. MILLISECOND, 0 );
Intent intent = new Intent (AlarmActivity. this, AlarmReceiver. class );
PendingIntent pendingIntent = PendingIntent. getBroadcast (AlarmActivity. this, 0, intent, 0 );
AlarmManager alarmManager = (AlarmManager) getSystemService (ALARM_SERVICE );
AlarmManager. set (AlarmManager. RTC_WAKEUP, calendar. getTimeInMillis (), pendingIntent );
AlarmManager. setRepeating (AlarmManager. RTC_WAKEUP, System. currentTimeMillis () + (10*1000 ),
(24x60x60x1000), pendingIntent );
String tmps = "set the alarm time to" + format (hourOfDay) + ":" + format (minute );
MTextView. setText (tmps );
}
}, Hour, minute, true). show ();
}
});

MButton2.setOnClickListener (new View. OnClickListener (){

Public void onClick (View v ){
// TODO Auto-generated method stub
Intent intent = new Intent (AlarmActivity. this, AlarmReceiver. class );
PendingIntent pendingIntent = PendingIntent. getBroadcast (AlarmActivity. this, 0, intent, 0 );
AlarmManager alarmManager = (AlarmManager) getSystemService (ALARM_SERVICE );
AlarmManager. cancel (pendingIntent );
MTextView. setText ("the alarm has been canceled! ");
}
});
}

Private String format (int time ){
String str = "" + time;
If (str. length () = 1 ){
Str = "0" + str;
}
Return str;
}
}

 

 

The effect is as follows:

 

Set alarm


 

 

 

Now the time is up to the Set alert time:

 

 

 

Cancel the alarm:

 




From Peking University-Google Android lab

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.