[英]How to associate an (AlarmManager) notification with an object it was associated with?
So the title is very uninformative because this issue is hard to describe. 由于这个问题很难描述,因此标题非常无用。 Essentially, I have a series of Alarm objects each with an id, time, name, etc. and when I created the PendingIntent for the AlarmManager i tagged the PendingIntent with the id of the Alarm it was associated with, so I could delete the alarm later (if the user decided to delete it). 本质上,我有一系列的警报对象,每个对象都有一个ID,时间,名称等。当我为AlarmManager创建PendingIntent时,我用与其关联的警报ID标记了PendingIntent,所以我可以删除警报以后(如果用户决定删除它)。 But when the Alarm goes off, I have a class AlarmReceiver.java
that extends WakefulBroadcastReceiver
and a method onReceive(final Context context, Intent intent)
但是当警报AlarmReceiver.java
时,我有一个扩展了WakefulBroadcastReceiver
的类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();
}
}
And here's where I create the alarm notifications with AlarmManager 这是我使用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();
}
My issue is when the Alarm goes off and onReceive is called, I don't know how to associate that Alarm with one of my Alarm objects so that I could pull information from it. 我的问题是当警报响起并调用onReceive时,我不知道如何将该警报与我的一个Alarm对象关联,以便从中获取信息。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks 谢谢
you don't need the ENTIRE alarm object for SOME information... 您不需要ENTIRE警报对象即可获取某些信息...
long answer short: put and get extra 长答案简短:投入并获得额外收益
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....
later on.. when dealing with onReceive 以后..当处理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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.