Android Phone Alarm Clock Usage Example _android

Source: Internet
Author: User
Tags current time

This example describes the use of the Android phone alarm clock. Share to everyone for your reference. Specifically as follows:

First, the development of mobile phone alarm clock mainly used in the Alarmmanager class, Alarmmanager class provides access to the system timer services, developers can set an application in the program at some point in the future to be executed. When the Alarmmanager time is up, the originally registered intent object will be broadcast by the system, and then the target program can be started.

When the Alarmmanager class needs to be used when the program is running, the Alarmmanager object can be obtained by the Getsystemservice (Context.alarm_service) method of the context object.

Use the Time Selection dialog box in the following program to set the time of the alarm.

Main.xml Layout file:

<?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" >
 <button android:id= "@+id/btn" android:text= 
  "set alarm"
  android:layout_width= "Fill_parent"
  android:layout_height= "wrap_content"/>
</LinearLayout>

Manifest file:

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android=
"http://schemas.android.com/apk/res/" Android "
 package=" com.ljq.activity "android:versioncode=" 1 "
 android:versionname=" 1.0 ">
 < Application android:icon= "@drawable/icon"
  android:label= "@string/app_name" >
  <activity android:name =". Mainactivity "
   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 "/>
  <!--android:process=": Remote ": Opening a new process-->
  <receiver android:name=". Alarmreceiver "android:process=": Remote "/>
 </application>
 <uses-sdk android:minsdkversion= "7"/>
</manifest>

Alarmreceiver class:

Package com.ljq.activity;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
The public class Alarmreceiver extends broadcastreceiver{
 @Override
 the public void onreceive (context context, Intent Intent) {
  Intent I=new Intent (context, alarmactivity.class);
  I.addflags (intent.flag_activity_new_task);
  Context.startactivity (i);
 }
}

Alarmactivity class:

Package com.ljq.activity;
Import android.app.Activity;
Import Android.app.AlertDialog;
Import Android.content.DialogInterface;
Import Android.content.DialogInterface.OnClickListener;
Import Android.os.Bundle;
public class Alarmactivity extends activity {
 @Override public
 void OnCreate (Bundle savedinstancestate) {
  Super.oncreate (savedinstancestate);
  Displays the dialog box
  new Alertdialog.builder (alarmactivity.this).
   Settitle ("Alarm Clock").//Set the title
   Setmessage ("Time is up!") ")./set Content
   Setpositivebutton (" Yes ", new Onclicklistener () {//Set button public
    void OnClick Dialoginterface dialog, int which) {
     AlarmActivity.this.finish ();//close Activity
    }
   }). Create (). Show ();
 }


Mainactivity class:

Package com.ljq.activity;
Import Java.util.Calendar;
Import android.app.Activity;
Import Android.app.AlarmManager;
Import Android.app.Dialog;
Import android.app.PendingIntent;
Import Android.app.TimePickerDialog;
Import Android.content.Context;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.TimePicker;
Import Android.widget.Toast;
 public class Mainactivity extends activity{private Button btn=null;
 Private Alarmmanager Alarmmanager=null;
 Calendar cal=calendar.getinstance (); Final int dialog_time = 0;
  Sets the dialog box ID @Override protected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.main);
  Alarmmanager= (Alarmmanager) Getsystemservice (Context.alarm_service);
  Btn= (Button) Findviewbyid (R.ID.BTN); Btn.setonclicklistener (New View.onclicklistener () {public void OnClick (view view) {ShowDialog (dialog_time);//Display time selection
 Optional dialog box}}); } @OvErride protected Dialog oncreatedialog (int id) {Dialog dialog=null; Switch (ID) {Case dialog_time:dialog=new Timepickerdialog (this, new Timepickerdialog.ontimesetlistener ( {public void Ontimeset (Timepicker timepicker, int hourofday,int minute) {Calendar c=calendar.getinstance ()  ;//Get Date Object C.settimeinmillis (System.currenttimemillis ());  Sets the Calendar object C.set (Calendar.hour, hourofday);   Set Alarm hours c.set (Calendar.minute, MINUTE);    Set the alarm number of minutes c.set (Calendar.second, 0);   Set the alarm number of seconds C.set (Calendar.millisecond, 0); Set the number of milliseconds for the alarm Intent Intent = new Intent (mainactivity.this, Alarmreceiver.class); Create Intent object Pendingintent pi = pendingintent.getbroadcast (mainactivity.this, 0, intent, 0);  Create Pendingintent//alarmmanager.set (Alarmmanager.rtc_wakeup, C.gettimeinmillis (), pi);  Set Alarm Clock Alarmmanager.set (Alarmmanager.rtc_wakeup, System.currenttimemillis (), pi); Set the alarm, the current time will wake Toast.maketext (MAinactivity.this, "Alarm clock set successfully", Toast.length_long). Show ()//Prompt user}, Cal.get (Calendar.hour_of_day), CA
   L.get (Calendar.minute), false);
  Break
 return dialog;

 }
}

Run Result:

I hope this article will help you with your Android program.

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.