Step by Step _android Development Course [33]_ User Interface Alarmmanager (global timer)

Source: Internet
Author: User

Focus on technology, enjoy life! --qq:804212028
Browse Links: http://blog.csdn.net/y18334702058/article/details/44624305

    • Topic: User Interface Alarmmanager (global timer)
      -

the role of Alarmmanager:

Alarmmanager objects are used in conjunction with intent, you can start an activity on a timed basis, send a broadcast, or open a service.

There are three common methods of Alarmmanager:

(1) Set (int type,long starttime,pendingintent pi);

This method is used to set a one-time alarm, the first parameter represents the alarm type, the second parameter indicates the alarm execution times, and the third parameter represents the alarm response action.

(2) setrepeating (int type,long Starttime,long
Intervaltime,pendingintent pi);

This method is used to set a repeating alarm, the first parameter represents the alarm type, the second parameter represents the first time the alarm is executed, the third parameter represents the interval between two executions of the alarm, and the third parameter represents the alarm response action.

(3) setinexactrepeating (int type,long Starttime,long
Intervaltime,pendingintent pi);

This method is also used to set a repeating alarm, similar to the second method, although the interval between the two alarms is not fixed.

Three methods each parameter keyholder:

(1) INT type: The type of alarm clock, commonly used has 5 values: Alarmmanager.elapsed_realtime,
Alarmmanager.elapsed_realtime_wakeup, ALARMMANAGER.RTC,
Alarmmanager.rtc_wakeup, Alarmmanager.power_off_wakeup.

Alarmmanager.elapsed_realtime means that the alarm clock is not available when the phone is asleep, and the alarm is used relative to the time (starting with the system boot) and the state value is 3;

Alarmmanager.elapsed_realtime_wakeup indicates that the alarm will wake up the system and perform the prompt function, the alarm clock also uses relative time, the status value is 2;

ALARMMANAGER.RTC indicates that the alarm is not available in sleep, and the alarm is used in absolute time, that is, the current system time, the status value is 1;

Alarmmanager.rtc_wakeup means that the alarm will wake up the system and perform the prompt function when the alarm is in the state of sleep, which has an absolute time and a state value of 0.

Alarmmanager.power_off_wakeup means that the alarm clock in the state of the hand machine can also be a normal prompt function, so it is one of the most used in 5 states, the alarm is also used absolute time, the status value of 4; But this state seems to be affected by the SDK version, Some editions are not supported;

(2) long startTime:
The first time the alarm is executed, in milliseconds, the time can be customized, but the current time is generally used. It is important to note that this property is closely related to the first property (type), if the first argument is to a
Elapsed_realtime and Elapsed_realtime_wakeup are used for the alarm clock, this property uses relative time (relative to
System start-up time), such as the current time is expressed as: Systemclock.elapsedrealtime (); If the alarm for the first parameter uses an absolute time
(RTC, Rtc_wakeup, Power_off_wakeup), this property uses absolute time, such as the current time to represent
is: System.currenttimemillis ().

(3) long intervaltime: For the latter two methods, this property exists, indicating the interval between two alarm executions, and in milliseconds.

(4) pendingintent pi:
An alarm is bound to perform actions such as sending a broadcast, giving hints, and so on. Pendingintent is the encapsulation class for intent. It is important to note that if you implement the alarm by starting the service
The Pendingintent object should be acquired using Pending.getservice (Context c,int i,intent
Intent,int j) method, if the alarm is realized by broadcasting, the Pendingintent object should be acquired using
Pendingintent.getbroadcast (Context c,int i,intent intent,int
J) method, if the alarm is implemented in an activity way, the acquisition of the Pendingintent object should take
Pendingintent.getactivity (Context c,int i,intent intent,int
J) method. If these three methods are wrong, although not error, but do not see the alarm effect.

Alarmmanager Global Timer (instance):

Achieve the effect: Alarmmanager timed to open an activity, send a broadcast and open a service.
Androidmanifest.xml:

<?xml version= "1.0" encoding= "Utf-8"?><manifest xmlns:android="Http://schemas.android.com/apk/res/android"  package ="COM.YFZ"android:versioncode= "1"android:versionname="1.0" >                <uses-sdk android:minsdkversion="7" />    <applicationandroid:icon="@drawable/ic_launcher"Android:label ="@string/app_name" >                        <activityAndroid:label= "@string/app_name"android:name=". Mainactivity " >                                    <intent-filter >                <action android:name="Android.intent.action.MAIN" />                <category android:name="Android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <receiverandroid:name="Actionbroadcast">                    </receiver>        <service android:name="Actionservice"></Service>        <activity android:name="actionactivity"></activity>    </Application></manifest>

Main.xml:

<?xml version= "1.0" encoding= "Utf-8"?><linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"  Android:layout_width="Fill_parent"android:layout_height="Fill_parent"  Android:orientation="vertical" >                <button  android:id  = "@+id/id_btn1"  android:layout_width  = "fill_parent"  android:layout_height  = "wrap_content"  android:text  =" broadcast broadcast "/>     <button  android:id  = "@+id/id_btn2"  android:layout_width  = "fill_parent"  android:layout_height  = "wrap_content"  android:text  =" service "/>     <buttonandroid:id="@+id/id_btn3"android:layout_width="Fill_ Parent "android:layout_height=" Wrap_content "android:text=" active activity " / >                                </linearlayout>

Mainactivity.java:

Import Android. App. Activity;Import Android. App. Alarmmanager;Import Android. App. Pendingintent;Import Android. Content. Intent;Import Android. OS. Bundle;Import Android. View. View;Import Android. View. View. Onclicklistener;Import Android. Widgets. Button;public class Mainactivity extends Activity {alarmmanager am;@Override public void OnCreate (Bundle savedinstancestate) {Super. OnCreate(savedinstancestate);Setcontentview (R. Layout. Main);am = (Alarmmanager) getsystemservice (Alarm_service);Button btn1 = (button) Findviewbyid (R. ID. ID_BTN1);Button btn2 = (button) Findviewbyid (R. ID. ID_BTN2);Button Btn3 = (button) Findviewbyid (R. ID. ID_BTN3);Btn1. Setonclicklistener(onclick);Btn2. Setonclicklistener(onclick);Btn3. Setonclicklistener(onclick);} onclicklistener onclick = new Onclicklistener () {@Override public void onclick (View v) {Lo ng now = System. Currenttimemillis();Pendingintent pi = null;Switch (v. GetId()) {case R. ID. ID_btn1:pi = Pendingintent. Getbroadcast(mainactivity. this,0, New Intent (mainactivity. this, Actionbroadcast. Class), Intent. FLAG_activity_new_task);                     Break;Case R. ID. ID_btn2:pi = Pendingintent. GetService(mainactivity. this,0, New Intent (mainactivity. this, Actionservice. Class), Intent. FLAG_activity_new_task);                     Break;Case R. ID. ID_btn3:pi = Pendingintent. Getactivity(mainactivity. this,0, New Intent (mainactivity. this, actionactivity. Class), Intent. FLAG_activity_new_task);                     Break;Default Break;} AM. setinexactrepeating(Alarmmanager. RTC_wakeup, now, the, pi);}    };}

Actionactivity.java:

Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.widget.Button; Public  class actionactivity extends Activity {    Private Static intnum =0;@Override    protected void onCreate(Bundle savedinstancestate) {//TODO auto-generated method stub        Super. OnCreate (Savedinstancestate); LOG.E ("Actionactivity","Activity New Message!"+ num++); }   }

Actionbroadcast.java:

ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;ImportAndroid.content.Intent;ImportAndroid.util.Log; Public  class actionbroadcast extends broadcastreceiver {    Private Static intnum =0;@Override     Public void OnReceive(context context, Intent Intent) {//TODO auto-generated method stubLOG.E ("Actionbroadcast","New Message!"+ num++); }}

Actionservice.java:

ImportAndroid.app.Service;ImportAndroid.content.Intent;ImportAndroid.os.IBinder;ImportAndroid.util.Log; Public  class actionservice extends Service {    Private Static intnum =0;@Override     PublicIBinderOnbind(Intent arg0) {//TODO auto-generated method stub        return NULL; }@Override     Public void OnStart(Intent Intent,intStartid) {//TODO auto-generated method stub        Super. OnStart (Intent, Startid); LOG.E ("Actionservice","Service New Message!"+ num++); }@Override     Public int Onstartcommand(Intent Intent,intFlagsintStartid) {//TODO auto-generated method stubLOG.E ("Actionservice","----------");return Super. Onstartcommand (Intent, flags, Startid); }}

Focus on technology, enjoy life! --qq:804212028
Browse Links: http://blog.csdn.net/y18334702058/article/details/44624305

Step by Step _android Development Course [33]_ User Interface Alarmmanager (global timer)

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.