Android timed Tasks

Source: Internet
Author: User

Objective

We may encounter some requirements in the normal development, for example, daily reminders, timing updates, and so on, anyway, is a recurring task, encounter such requirements, we can be called scheduled tasks. We used to do this by using thread handler, now that it's on Android, it's even better, and Android gives us a alarmmanager. It is a system-level timing prompt service that can be run outside of the app's life cycle, or powerful.

Demand

If we have a need now, we have to perform a task at 7 o ' Day every morning and execute it once in a while.

Coding

First of all must be a receiver (radio) to carry out the task, the content is relatively simple, is toast a word

public class AlramReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context,"定时任务开始了",Toast.LENGTH_SHORT).show(); }}

Don't forget to configure it in Androidmanifest.

Next, in our main interface, we're going to start this scheduled task.

PublicClassMainactivityExtendsappcompatactivity {Private Button Mopen;@OverrideProtectedvoidOnCreate (Bundle savedinstancestate) {Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Mopen= (Button) Findviewbyid (R.ID.OPEN_BTN); Mopen.setonclicklistener (New View.onclicklistener () {@OverridePublicvoidOnClick (View v) {Alarmmanager manager = (Alarmmanager) getsystemservice (Alarm_service); Calendar calendar=calendar.getinstance (); Calendar.set (Calendar.minute,R); Calendar.set (Calendar.second, 0); Calendar.set (Calendar.millisecond, 0); Calendar.set (Calendar.hour_of_day ,7); int interval=1000*60*;  One-hour interval long triggerattime = System.currenttimemillis (); Intent i = new Intent (this, alramreceiver.class); pendingintent pi = pendingintent.getbroadcast (this, 0, I, 0); Manager.setrepeating (alarmmanager.rtc_ WAKEUP, Triggerattime,interval, pi); } }); }}

Next, I'll explain in more detail how some of the methods in 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 triggerattime,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.

Type: Alarm types with five selectable values

1.ELAPSED_REALTIME:以手机开机的时间为基准2.ELAPSED_REALTIME_WAKEUP:以手机开机的时间为基准,并且可以在休眠时发出广播3.RTC:以UTC标准时间为基准4.RTC_WAKEUP:以UTC标准时间为基准,并且可以在休眠时发出广播。这种方式是最常见的形式。5.POWER_OFF_WAKEUP:关机状态下也能进行提示

Long startTime:

Indicates the time that the alarm was first executed, can be set by itself, or the current time of the system, in milliseconds. This property is associated with section
A property (type) is closely related, if the first parameter corresponds to an alarm that uses a relative time (Elapsed_realtime and Elapsed_realtime_wakeup), then this property has to use relative time (relative to the system boot time to
Say), such as the current time is expressed as: Systemclock.elapsedrealtime (); If the first parameter corresponds to an alarm that uses an absolute time (RTC, Rtc_wakeup, Power_off_wakeup), Then this property will have to use absolute time, such as the current time is expressed as: System.currenttimemillis ().

Long intervaltime:
The interval of the alarm clock is also the millisecond.

Pendingintent PI:
This is what we can do, we can start a service, send a broadcast, start an activity, the way to see it.
One last tip:
Calendar.hour_of_day (24 hours)
Calendar.hour (12 hours)

Android timed Tasks

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.