Android timer start \ Cancel Small Example _android

Source: Internet
Author: User
Copy Code code as follows:

Intent Intent = new Intent ("Cn.pocketdigi.update.alarm");
Intent.setclass (this, alarmreceiver.class);
Pendingintent Pi=pendingintent.getbroadcast (this, 0, intent,0);
Set a Pendingintent object to send a broadcast
Alarmmanager am= (Alarmmanager) Getsystemservice (Alarm_service);
Get Alarmmanager Object
Am.set (Alarmmanager.rtc_wakeup, System.currenttimemillis () +3500, pi);//Only once
Am.setrepeating (Alarmmanager.rtc_wakeup, System.currenttimemillis () +3500, 10000, PI); Repeat execution

Copy Code code as follows:

<receiver android:name= ". Alarmreceiver ">
<intent-filter>
<action android:name= "Cn.pocketdigi.update.alarm"/>
</intent-filter>
</receiver>

Copy Code code as follows:

public class Alarmreceiver extends Broadcastreceiver {
private static final String TAG = "Alarmreceiver";
@Override
public void OnReceive (context context, Intent Intent) {
TODO auto-generated Method Stub
LOG.V (TAG, "received the broadcast");
Intent it=new Intent (context,alarmactivity.class);
It.addflags (Intent.flag_activity_new_task);
Context.startactivity (IT);
After receiving the broadcast, start the activity, for simplicity, jump directly to the activity setting up alarm.
Intent must add Intent.flag_activity_new_task FLAG
}
}

Cancellation Mode:
Copy Code code as follows:

Intent Intent = new Intent ("Cn.pocketdigi.update.alarm");
Intent.setclass (this, alarmreceiver.class);
Pendingintent Pi=pendingintent.getbroadcast (this, 0, intent,0);
Alarmmanager alarm= (Alarmmanager) Getsystemservice (Alarm_service);
Alarm.cancel (PI);

Another kind of intent way
Code
Copy Code code as follows:

Intent Intent =new-Intent (Main.this, Alarmreceiver.class);
Intent.setaction ("repeating");
Pendingintent sender=pendingintent
. Getbroadcast (main.this, 0, intent, 0);
//Start time
Long Firstime=systemclock.elapsedrealtime ();  
Alarmmanager am= (Alarmmanager) Getsystemservice (Alarm_service); 5 Seconds a cycle, Non-stop send broadcast
am.setrepeating (Alarmmanager.elapsed_realtime_wakeup
, firstime, 5*1000, sender);

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.