UseAlarmManagerTo set your own alarm, among which the main classes we use are
- import android.app.AlarmManager;
- import android.app.PendingIntent;
- 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:
- Package eoe. demo;
- Import android. content. BroadcastReceiver;
- Import android. content. Context;
- Import android. content. Intent;
- Import android. widget. Toast;
- Public class AlamrReceiver extends BroadcastReceiver {
- @ Override
- Public void onReceive (Context context, Intent intent ){
- // TODO Auto-generated method stub
- Toast. makeText (context, "alarm time to", Toast. LENGTH_LONG). show ();
- }
- }
Java code:
- Package eoe. demo;
- 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 Test extends Activity {
- /** Called when the activity is first created .*/
- Private TextView TV = null;
- Private Button btn_set = null;
- Private Button btn_cel = null;
- Private Calendar c = null;
- @ Override
- Public void onCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. main );
- TV = (TextView) this. findViewById (R. id. TextView );
- Btn_set = (Button) this. findViewById (R. id. Button01 );
- Btn_cel = (Button) this. findViewById (R. id. Button02 );
- C = Calendar. getInstance ();
- Btn_set.setOnClickListener (new Button. OnClickListener (){
- Public void onClick (View v ){
- // TODO Auto-generated method stub
- C. setTimeInMillis (System. currentTimeMillis ());
- Int hour = c. get (Calendar. HOUR_OF_DAY );
- Int minute = c. get (Calendar. MINUTE );
- New TimePickerDialog (Test. this, new TimePickerDialog. OnTimeSetListener (){
- Public void onTimeSet (TimePicker view, int hourOfDay, int minute ){
- // TODO Auto-generated method stub
- C. setTimeInMillis (System. currentTimeMillis ());
- C. set (Calendar. HOUR_OF_DAY, hourOfDay );
- C. set (Calendar. MINUTE, minute );
- C. set (Calendar. SECOND, 0 );
- C. set (Calendar. MILLISECOND, 0 );
- Intent intent = new Intent (Test. this, AlamrReceiver. class );
- PendingIntent pi = PendingIntent. getBroadcast (Test. this, 0, intent, 0 );
- AlarmManager am = (AlarmManager) getSystemService (Activity. ALARM_SERVICE );
- Am. set (AlarmManager. RTC_WAKEUP, c. getTimeInMillis (), pi); // set the alarm
- Am. setRepeating (AlarmManager. RTC_WAKEUP, c. getTimeInMillis (), (10*1000), pi); // repeat settings
- TV. setText ("the set alarm time is:" + hourOfDay + ":" + minute );
- }
- }, Hour, minute, true). show ();
- }
- });
- Btn_cel.setOnClickListener (new Button. OnClickListener (){
- Public void onClick (View v ){
- // TODO Auto-generated method stub
- Intent intent = new Intent (Test. this, AlamrReceiver. class );
- PendingIntent pi = PendingIntent. getBroadcast (Test. this, 0, intent, 0 );
- AlarmManager am = (AlarmManager) getSystemService (Activity. ALARM_SERVICE );
- Am. cancel (pi );
- TV. setText ("Cancel alarm ");
- }
- });
- }
- }
Java code:
- <? Xml version = "1.0" encoding = "UTF-8"?>
- <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
- Android: orientation = "vertical" android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent">
- <TextView android: layout_width = "fill_parent"
- Android: id = "@ + id/TextView"
- Android: layout_height = "wrap_content" android: text = "@ string/hello"/>
- <Button android: text = "Set alert Clock" android: id = "@ + id/Button01"
- Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content">
- </Button>
- <Button android: text = "Cancel alarm" android: id = "@ + id/Button02"
- Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content">
- </Button>
- </LinearLayout>
Java code:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="eoe.demo"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".Test"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <receiver android:name=".AlamrReceiver" android:process=":remote"></receiver>
- </application>
- </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