首先,设置:
Intent i = new Intent( context.getApplicationContext(), NameOfYourClass.class );
PendingIntent pi = PendingIntent.getBroadcast( c.getApplicationContext(), 0, i, 0 );
AlarmManager am = (AlarmManager) context.getSystemService( Context.ALARM_SERVICE );
am.setRepeating( AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), 5*60*1000, pi );
// ELAPSED_REALTIME will execute ONLY when phone is awake, to execute always use ELAPSED_REALTIME_WAKEUP
就在此初始设置之后,当计数器启动时保存:,例如。偏好管理器
最后,时机成熟:
Intent i = new Intent( context.getApplicationContext(), NameOfYourClass.class );
PendingIntent pi = PendingIntent.getBroadcast( context.getApplicationContext(), 0, i, 0 );
AlarmManager am = (AlarmManager) context.getSystemService( Context.ALARM_SERVICE );
am.cancel( pi );