1、问题描述:如题;
2、问题分析:在/DeskClock/src/com/android/deskclock/alarms/AlarmService.java文件的public int onStartCommand(Intent intent, int flags, int startId)方法中对这种重复设计相同时间的闹钟没有做出处理。我们只需要在闹钟响起的的时候判断当前闹钟是不是已经是正在响的闹钟,如果不是说明是设置了多个同时间的闹钟,这时候的处理是不重新响铃。将这种重复的闹钟过滤掉。
3、解决方案:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v("AlarmService.onStartCommand() with intent: " + intent.toString());
long instanceId = AlarmInstance.getId(intent.getData());
if (START_ALARM_ACTION.equals(intent.getAction())) {
ContentResolver cr = this.getContentResolver();
AlarmInstance instance = AlarmInstance.getInstance(cr, instanceId);
if (instance == null) {
Log.e("No instance found to start alarm: " + instanceId);
if (mCurrentAlarm != null) {
// Only release lock if we are not firing alarm
AlarmAlertWakeLock.releaseCpuLock();
}
return Service.START_NOT_STICKY;
} else if (mCurrentAlarm != null && mCurrentAlarm.mId == instanceId) {
Log.e("Alarm already started for instance: " + instanceId);
return Service.START_NOT_STICKY;
}
//added
else if (mCurrentAlarm != null && mCurrentAlarm.mId != instanceId) {
Log.e("Alarm was started for instance: " + instanceId);
AlarmStateManager.setDismissState(this, instance);
return Service.START_NOT_STICKY;
}
//added end
if (!phoneIsInUse()) {
startAlarm(instance);
} else {
AlarmStateManager.setSnoozeState_incall(this, instance);
}
} else if(STOP_ALARM_ACTION.equals(intent.getAction())) {
if (mCurrentAlarm != null && mCurrentAlarm.mId != instanceId) {
Log.e("Can't stop alarm for instance: " + instanceId +
" because current alarm is: " + mCurrentAlarm.mId);
return Service.START_NOT_STICKY;
}
stopSelf();
}
return Service.START_NOT_STICKY;
}
希望我的博客能对你有用,写博客的用意是自己做一个记录和自己的知识能在某个时候帮到大家。