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);