将意图额外传递给android广播接收器
Posted
技术标签:
【中文标题】将意图额外传递给android广播接收器【英文标题】:passing intent extra to android broadcast receiver 【发布时间】:2012-01-15 15:30:45 【问题描述】:我创建了一个 AlarmReceiver 类,用作警报的广播接收器。问题是我需要从设置警报的类发送一些值到广播接收器类。
setAlarmManager.java
Intent i = new Intent(mContext, AlarmReceiver.class);
i.putExtra(KEY_ROWID, (long)taskId);
PendingIntent pi = PendingIntent.getBroadcast(mContext,taskId_int,i,PendingIntent.FLAG_ONE_SHOT);
mAlarmManager.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pi);
我需要从 alarmreceiver 类的意图中获取 KEY_ROWID。我怎样才能做到这一点? AlarmReceiver 类如下所示。
public class AlarmReceiver extends BroadcastReceiver
public static final String ALARM_ALERT_ACTION ="com.android.alarmclock.ALARM_ALERT";
public static final String ALARM_INTENT_EXTRA = "intent.extra.alarm";
@Override
public void onReceive(Context context, Intent intent)
//Here I need the values from intent using bundle or anything...
【问题讨论】:
你如何把时间价值@John? 【参考方案1】:你可以使用:
public class AlarmReceiver extends BroadcastReceiver
public static final String ALARM_ALERT_ACTION ="com.android.alarmclock.ALARM_ALERT";
public static final String ALARM_INTENT_EXTRA = "intent.extra.alarm";
@Override
public void onReceive(Context context, Intent intent)
String keyid = intent.getStringExtra("KEY_ROWID");
【讨论】:
以上是关于将意图额外传递给android广播接收器的主要内容,如果未能解决你的问题,请参考以下文章