AlarmManager provides a system-level prompt service that allows you to execute a service at a certain time in the future. The AlarmManager object is generally obtained through the Context. getSystemService (Context. ALARM_SERVICE) method.
The following is an example for better understanding: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PC9wPgo8cHJlIGNsYXNzPQ = "brush: java;"> package com. app; import com. app. r; 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. widget. button;/***** test AlarmManager */public class MainActivity extends Activity {// declare Butt Onprivate Button setBtn, cancelBtn; // defines the broadcast Actionprivate static final String BC_ACTION = "com. action. BC_ACTION "; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // sets the current Layout View setContentView (R. layout. main); // instantiate ButtonsetBtn = (Button) findViewById (R. id. button01); cancelBtn = (Button) findViewById (R. id. button02); // obtain the AlarmManager instance final AlarmManager am = (Alar MManager) getSystemService (ALARM_SERVICE); // instantiate IntentIntent intent = new Intent (); // set Intent action attribute intent. setAction (BC_ACTION); intent. putExtra ("msg", "you should go to the meeting! "); // Instantiate PendingIntentfinal PendingIntent pi = PendingIntent. getBroadcast (MainActivity. this, 0, intent, 0); // obtain the System time final long time = System. currentTimeMillis (); // click the setBtn event button. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// repeat the prompt, starting from the current time, at an interval of 5 seconds am. setRepeating (AlarmManager. RTC_WAKEUP, time, 5*1000, pi) ;}}); // click the event cancelBtn button. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {am. cancel (pi );}});}}
Note: A static method of PendingIntent,
GetBroadcast
public static PendingIntent getBroadcast(Context context, int requestCode, Intent intent, int flags)
Retrieve a PendingIntent that will perform a broadcast, like calling
Context.sendBroadcast()
.
Parameters:
context
-The Context in which this PendingIntent shocould perform the broadcast.
requestCode
-Private request code for the sender (currently not used ).
intent
-The Intent to be broadcast.
flags
-May be
FLAG_ONE_SHOT
,
FLAG_NO_CREATE
,
FLAG_CANCEL_CURRENT
,
FLAG_UPDATE_CURRENT
, Or any of the flags as supported
Intent.fillIn()
To control which unspecified parts of the intent that can be supplied when the actual send happens.
Return Value:Returns an existing or new PendingIntent matching the given parameters. May return null only if
FLAG_NO_CREATE
Has been supplied.
Package com. app; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. widget. toast; public class MyReceiver extends BroadcastReceiver {@ Overridepublic void onReceive (Context context, Intent intent) {// get the prompt message String msg = intent. getStringExtra ("msg"); // display the Toast message. makeText (context, msg, Toast. LENGTH_LONG ). show ();}}
Do not forget to register MyReceiver in AndroidManifest. xml:
Result: