我正在开发一个可以有多个警报的闹钟应用程序。因此,我一直试图使用Alarm
类来构建每个警报,并将每个警报的AlarmManager
和PendingIntent
附加到该实例。
我只需要创建一个没有Alarm类的警报就可以工作,但是现在我希望能够扩大警报范围。数据似乎传输良好,但是AlarmReceiver
本身从未触发。
Alarm.java :
public class Alarm implements Serializable {
String name;
int hour;
int minute;
boolean isPm;
int[] daysActive;
double snoozeTimer;
AlarmManager alarmManager;
PendingIntent pendingIntent;
public Alarm(String startName, int startHour, int startMinute,
Boolean startIsPm, int[] startDaysActive, double startSnoozeTimer){
name = startName;
hour = startHour;
minute = startMinute;
isPm = startIsPm;
daysActive = startDaysActive;
snoozeTimer = startSnoozeTimer;
}
在 CreateAlarmActivity.java 中创建一个新警报,并在此处完成。我现在只关注警报的创建,而不是更新警报。
public void finishAlarm(View view){
EditText alarmName = findViewById(R.id.alarmName);
String name = alarmName.getText().toString();
Alarm alarm = new Alarm(name, alarmHour, alarmMinute, isPm, daysActive, snoozeTimer);
Intent finishAlarmIntent = new Intent(this, MainActivity.class);
// If we're creating an alarm.
if (position == -1) {
try {
alarmList.add(alarm);
finishAlarmIntent.putExtra("setAlarm", alarm);
} catch (Exception e) {
}
}
// Else, we're updating one.
else {
try {
// Set the alarm we're updating to the new alarm.
alarmList.set(position, alarm);
finishAlarmIntent.putExtra("updateAlarm", alarm);
} catch (Exception e) {
}
}
// Head back to MainActivity.
finishAlarmIntent.putExtra("alarmList", alarmList);
startActivity(finishAlarmIntent);
}
将警报传递到setAlarm
下的意图后,然后我们前往MainActivity
,看看是否需要设置警报。
Intent intent = getIntent();
// If a new alarm was passed (created or updated) set the alarm.
if (intent != null) {
System.out.println("Intent not null.");
if (intent.hasExtra("setAlarm")){
Alarm alarm = (Alarm)getIntent().getSerializableExtra("setAlarm");
System.out.println("We have isSetAlarm. Alarm hour = " + alarm.hour);
setAlarm(alarm);
} else {
// Do something else
}
}
最后,它调用setAlarm
方法。
public void setAlarm(Alarm alarm){
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
// HOUR_OF_DAY is on 24 hour time, so if isPm is true, add 12 hours.
if (alarm.isPm){
calendar.set(Calendar.HOUR_OF_DAY, alarm.hour + 12);
}
else {
calendar.set(Calendar.HOUR_OF_DAY, alarm.hour);
}
calendar.set(Calendar.MINUTE, alarm.minute);
alarm.alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent testIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
alarm.pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, testIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarm.alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, calendar.getTimeInMillis(), alarm.pendingIntent);
System.out.println("Setting alarm. Alarm set at " + alarm.hour + ":" + alarm.minute + ". IsPM = " + alarm.isPm);
}
来自System.out.println
的最终“设置警报”消息运行正常。闹钟小时,分钟和下午都正确。但是,当警报触发时,什么也不会发生。当我直接在AlarmReceiver
中创建警报时,MainActivity.
正常工作
AlarmReceiver.java (我知道将对象从字节流转换为字节流然后再回到字节流是一个坏主意,而不仅仅是按原样发送它,但是如果我不这样做,这个,我经常会出错。)
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
System.out.println("Beginning onReceive()");
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
Ringtone r = RingtoneManager.getRingtone(context, notification);
//r.play();
ByteArrayInputStream bis = new ByteArrayInputStream(intent.getByteArrayExtra("alarm"));
ObjectInputStream in = null;
Alarm alarm = null;
try {
in = new ObjectInputStream(bis);
alarm = (Alarm)in.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
Intent receiveIntent = new Intent(context, AlarmActivatedActivity.class);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out;
try {
out = new ObjectOutputStream(bos);
out.writeObject(alarm);
out.flush();
byte[] data = bos.toByteArray();
receiveIntent.putExtra("alarm", data);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bos.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
receiveIntent.setFlags(FLAG_ACTIVITY_NEW_TASK);
context.startActivity(receiveIntent);
}
}
AlarmActivatedActivity 当前只是一个UI,除了让您返回 MainActivity 之外没有其他功能,因此不重要。
这应该是所有相关的代码,但是如果需要更多的代码,这是具有最新更改的存储库。应该是公开的。 http://gitlab.csu.edu.au/Jay_Bailey/SnoozeProofAlarmClock