所以标题非常缺乏信息,因为这个问题很难描述。基本上,我有一系列的Alarm对象,每个都有id,时间,名称等。当我为AlarmManager创建PendingIntent时,我用PendingIntent标记了它与之关联的警报的id,所以我可以删除警报稍后(如果用户决定删除它)。但是当闹钟响起时,我有一个AlarmReceiver.java
的课程WakefulBroadcastReceiver
和一个方法onReceive(final Context context, Intent intent)
package org.rtsd.morningtalk;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.widget.Toast;
/**
* Created by Zig on 1/18/2016.
*/
public class AlarmReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent){
//Call the Text-to-Speech software
//Perform any actions necessary
Toast.makeText(context,
"BEEP!",
Toast.LENGTH_SHORT).show();
}
}
我在这里使用AlarmManager创建警报通知
if (!alarmExists(alarm)) {
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, alarm.getId(), intent, 0);
if (alarm.isDaily() || alarm.isWeekly()) {
dbHandler.createAlarm(alarm);
Alarms.add(alarm);
alarmAdapter.notifyDataSetChanged();
if (alarm.isDaily())
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, AlarmOrganizerTools.getAlarmTime(alarm), TimeUnit.DAYS.toMillis(1), pendingIntent);
else if (alarm.isWeekly())
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, AlarmOrganizerTools.getAlarmTime(alarm), TimeUnit.DAYS.toMillis(7), pendingIntent);
updateNextAlarm();
}
else {
alarmManager.set(AlarmManager.RTC_WAKEUP, AlarmOrganizerTools.getAlarmTime(alarm), pendingIntent);
}
Toast.makeText(getApplicationContext(),
"Your Alarm has been created!",
Toast.LENGTH_SHORT).show();
resetAlarmCreator();
updateNextAlarm();
}
我的问题是当闹钟响起并且调用onReceive时,我不知道如何将该警报与我的一个警报对象相关联,以便我可以从中提取信息。任何帮助,将不胜感激。感谢
答案 0 :(得分:0)
您不需要ENTIRE警报对象来获取某些信息......
长答案简短:放好并获得额外的
if (!alarmExists(alarm))
{
intent.putExtra("someKey", "some information here");
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, alarm.getId(), intent, 0);
if (alarm.isDaily() || alarm.isWeekly()) {
dbHandler.createAlarm(alarm);
Alarms.add(alarm);
// rest of code here....
稍后......处理onReceive
时@Override
public void onReceive(final Context context, Intent intent){
String tada = intent.getStringExtra("someKey");
// do something with that information
Toast.makeText(context,
"BEEP!",
Toast.LENGTH_SHORT).show();
}