Home>

First, the alarmmanager class is mainly used to develop mobile alarm clocks. The alarmmanager class provides a way to access the system's timing services.Developers can set an application to be executed at some point in the future in the program.When the alarmmanager time is up,The registered intent object will be broadcast by the system.The target program is then started.

Second, when the program needs to use the alarmmanager class, you can get the alarmmanager object through the getsystemservice (context.alarm_service) method of the context object.

The following procedure uses the time selection dialog 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":start 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;
public class alarmreceiver extends broadcastreceiver {
 @override
 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);
  //Show dialog
  new alertdialog.builder (alarmactivity.this).
   settitle ("Alarm Clock"). //Set the title
   setmessage ("Time is up!"). //Set the content
   setpositivebutton ("Know", new onclicklistener () {//Set button
    public void onclick (dialoginterface dialog, int which) {
     alarmactivity.this.finish ();//Close the 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;//Set dialog 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);//Show time selection dialog
   }
  });
 }
 @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 ());//Set the calendar object
       c.set (calendar.hour, hourofday);//Set the alarm hours
       c.set (calendar.minute, minute);//Set the minutes of the alarm
       c.set (calendar.second, 0);//Set the seconds of the alarm
       c.set (calendar.millisecond, 0);//Set the number of milliseconds for the alarm
       intent intent=new intent (mainactivity.this, alarmreceiver.class);//Create an intent object
       pendingintent pi=pendingintent.getbroadcast (mainactivity.this, 0, intent, 0);//create pendingintent
       //alarmmanager.set(alarmmanager.rtc_wakeup, c.gettimeinmillis (), pi);//Set the alarm
       alarmmanager.set (alarmmanager.rtc_wakeup, system.currenttimemillis (), pi);//Set the alarm,Wake up at current time
       toast.maketext (mainactivity.this, "The alarm was set successfully", toast.length_long) .show ();//Prompt the user
      }
     },     cal.get (calendar.hour_of_day),     cal.get (calendar.minute),     false);
   break;
  }
  return dialog;
 }
}

operation result:

  • Previous Android implements method to add shortcut to Launcher
  • Next Briefly introduce the application of category in iOS development