我使用AlarmManager和PendingIntent设置了闹钟。该应用程序有文本计数器,这是一个典型的计时器时钟向后计数,直到它达到0.我想知道是否已设置警报,以便我可以更新我的文本计数器和相应的开始/停止按钮状态,如果我的活动被杀死并恢复。
问题/问题:
来自previous question的已使用解决方案,但似乎无法正常工作。任何帮助都会很棒。
protected void onCreate(Bundle savedInstanceState) {
....
boolean isAlarmActive = (PendingIntent.getBroadcast(MainActivity.this, 0,
new Intent(INTENT_ALARM_ACTION),
PendingIntent.FLAG_NO_CREATE) != null);
if(isAlarmActive)
changeButtonStatus(ButtonStatus.STOP);
else
changeButtonStatus(ButtonStatus.STARt);
}
private OnClickListener mBtnListener = new OnClickListener()
{
@Override
public void onClick(View v) {
if(mButtonStatus == ButtonStatus.START){
setAlarmOnAlarmManager();
changeButtonStatus(ButtonStatus.STOP);
}else{
cancelAlarmOnAlarmManager();
changeButtonStatus(ButtonStatus.START);
}
}
public void setAlarmOnAlarmManager()
{
Calendar cal = Calendar.getInstance();
long triggerTime = cal.getTimeInMillis() + mAlarmInterval;
mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mPendingIntent);
}
public void cancelAlarmOnAlarmManager()
{
MainActivity.this.getSystemService(Context.ALARM_SERVICE);
mAlarmManager.cancel(mPendingIntent);
unregisterReceiver(mAlarmReceiver);
}