Android Development Instance alert, android instance alert
In this example, you can set an alarm in the TimePickerDialog Time Selection dialog box and use the global timer of AlarmManager to start the alarm Activity at the specified time.
Program running:
Instance code:
Package com. jph. alarm. test; import java. util. calendar; import android. app. activity; import android. app. alarmManager; import android. app. pendingIntent; import android. app. service; import android. app. timePickerDialog; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. timePicker; import Droid. widget. toast;/*** Describe: </br> * </br> simple alarm reminder * </br> in this instance, the TimePickerDialog Time Selection dialog box * </br> allows you to set an alarm and use the AlarmManager global timer * </br> Start the alarm Activity * </br> @ author jph * </br> Date: 2014.07.29 **/public class AlarmTest extends Activity {Button setTime; AlarmManager aManager; Calendar currentTime = Calendar. getInstance (); @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedIns TanceState); setContentView (R. layout. main); // get the setTime = (Button) findViewById (R. id. setTime); // get the AlarmManager object aManager = (AlarmManager) getSystemService (Service. ALARM_SERVICE); // bind the listener to the "set alarm" button. SetTime. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View source) {Calendar currentTime = Calendar. getInstance (); // create a TimePickerDialog instance and display it. New TimePickerDialog (AlarmTest. this, 0, // bind the listener new TimePickerDialog. onTimeSetListener () {@ Overridepublic void onTimeSet (TimePicker tp, int hourOfDay, int minute) {// specify to start the AlarmActivity component Intent intent = new Intent (); intent. setAction ("com. jph. alarm. test. ACTION "); // create the PendingIntent object PendingIntent pi = PendingIntent. getActivity (AlarmTest. this, 0, intent, 0); Calendar c = Calendar. getInstance (); // set the Calendar Object c. set (Calendar. HOUR_OF_DAY, hourOfDay); c. set (Calendar. MINUTE, minute); // set AlarmManager to start the specified component aManager at the specified time in the Calendar. set (AlarmManager. RTC_WAKEUP, c. getTimeInMillis (), pi); // display the message Toast when the alarm is set successfully. makeText (AlarmTest. this, "the alarm is set successfully", Toast. LENGTH_SHORT ). show () ;}, currentTime. get (Calendar. HOUR_OF_DAY), currentTime. get (Calendar. MINUTE), false ). show ();}});}}
Package com. jph. alarm. test; import java. util. calendar; import android. app. activity; import android. app. alarmManager; import android. app. pendingIntent; import android. app. service; import android. app. timePickerDialog; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. timePicker; import Droid. widget. toast;/*** Describe: </br> * </br> simple alarm reminder * </br> in this instance, the TimePickerDialog Time Selection dialog box * </br> allows you to set an alarm and use the AlarmManager global timer * </br> Start the alarm Activity * </br> @ author jph * </br> Date: 2014.07.29 **/public class AlarmTest extends Activity {Button setTime; AlarmManager aManager; Calendar currentTime = Calendar. getInstance (); @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedIns TanceState); setContentView (R. layout. main); // get the setTime = (Button) findViewById (R. id. setTime); // get the AlarmManager object aManager = (AlarmManager) getSystemService (Service. ALARM_SERVICE); // bind the listener to the "set alarm" button. SetTime. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View source) {Calendar currentTime = Calendar. getInstance (); // create a TimePickerDialog instance and display it. New TimePickerDialog (AlarmTest. this, 0, // bind the listener new TimePickerDialog. onTimeSetListener () {@ Overridepublic void onTimeSet (TimePicker tp, int hourOfDay, int minute) {// specify to start the AlarmActivity component Intent intent = new Intent (); intent. setAction ("com. jph. alarm. test. ACTION "); // create the PendingIntent object PendingIntent pi = PendingIntent. getActivity (AlarmTest. this, 0, intent, 0); Calendar c = Calendar. getInstance (); // set the Calendar Object c. set (Calendar. HOUR_OF_DAY, hourOfDay); c. set (Calendar. MINUTE, minute); // set AlarmManager to start the specified component aManager at the specified time in the Calendar. set (AlarmManager. RTC_WAKEUP, c. getTimeInMillis (), pi); // display the message Toast when the alarm is set successfully. makeText (AlarmTest. this, "the alarm is set successfully", Toast. LENGTH_SHORT ). show () ;}, currentTime. get (Calendar. HOUR_OF_DAY), currentTime. get (Calendar. MINUTE), false ). show ();}});}}
AndroidManifest. xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.jph.alarm.test"android:versionCode="1"android:versionName="1.0"><uses-sdkandroid:minSdkVersion="7"android:targetSdkVersion="18" /><application android:icon="@drawable/ic_launcher" android:label="@string/app_name"><activity android:name=".AlarmTest" android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name=".AlarmActivity"> <intent-filter> <action android:name="com.jph.alarm.test.ACTION"/> <category android:name="android.intent.category.DEFAULT"></category></intent-filter></activity></application></manifest>
How does android implement an alert clock with music reminders? (It must be a music Reminder)
It takes a lot of time to write code, so I am too lazy to write it. Let's talk about the idea. You should create a class to inherit the subbroadcastreceiver and rewrite the onReceive method, write the code for listening to the system time in it to match the time you set for the alarm. When matching, create a MediaPlayer object to play the music you want to play, music files can be stored in the Res/raw directory, but different methods can be used in the SD card. Look for an example of an Activity with an alarm. It is not difficult to write it out.
Android development, alert clock problem, how can I set an alert clock at every day to solve the problem of immediate start? Code:
The time you set has passed. That is to say, your sentence has logic problems.
If (calendar. getTimeInMillis ()> calendar. getTimeInMillis ()){
Calendar. set (Calendar. DAY_OF_YEAR, calendar. get (Calendar. DAY_OF_YEAR) + 1 );
}
Do you want to write it like this?
If (System. currentTimeMills ()> calendar. getTimeInMillis ()){...}
There is another point you should pay attention.
The first sentence should be written like this.
Calendar calendar = Calendar. getInstance (Locale. getDefault ());