Android:无法保存和读取捆绑包中的所有值
我有以下方法,将三个值保存到附加到已创建报警的intent中Android:无法保存和读取捆绑包中的所有值,android,android-intent,broadcastreceiver,bundle,android-alarms,Android,Android Intent,Broadcastreceiver,Bundle,Android Alarms,我有以下方法,将三个值保存到附加到已创建报警的intent中 public static Boolean setUniqueAlarm(Long alarmId, String occurenceTime, Context context) { Boolean saveResult = null; try { DateTime dt = TimeHelper.getDateTimeObject(occurenceTime);
public static Boolean setUniqueAlarm(Long alarmId, String occurenceTime, Context context) {
Boolean saveResult = null;
try {
DateTime dt = TimeHelper.getDateTimeObject(occurenceTime);
Logger.d("Year: " + dt.getYear() + ", month: " + dt.getYear() + ", day: " + dt.getDayOfMonth() + ", hour: " + dt.getHourOfDay() + ", minute: " + dt.getMinuteOfHour());
Logger.d("Occurrence time to save: "+occurenceTime);
AlarmManager alarmManager = (AlarmManager) context.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(context, AlarmBroadcastReceiver.class);
// Pass the intent type and other additional values into bundle
Bundle bundle = new Bundle();
bundle.putString(Constants.Global.ALARM_TYPE, Constants.Global.ALARM_TYPE_UNIQUE);
bundle.putString(Constants.Global.ALARM_OCCURRENCE_TIME, "123456");
bundle.putLong(Constants.Global.ALARM_UNIQUE_ID, alarmId);
alarmIntent.putExtras(bundle);
PendingIntent pendingAlarmIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, dt.getMillis(), pendingAlarmIntent);
saveResult = true;
} catch (Exception e) {
Logger.e(e.getMessage());
saveResult = false;
}
return saveResult;
}
在接收器中,我有以下代码:
@Override
public void onReceive(Context context, Intent intent) {
try {
Logger.d("ALARM RECEIVED!!!");
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
// Device battery life will be significantly affected by the use of this API.
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "TAG");
// Acquire the lock
wl.acquire();
//Release the lock
wl.release();
// Get the intent type (or stored variables)
Bundle extras = intent.getExtras();
mAlarmType = extras.getString(Constants.Global.ALARM_TYPE);
mAlarmId = extras.getLong(Constants.Global.ALARM_UNIQUE_ID, 0);
mOccurenceTime = extras.getString(Constants.Global.ALARM_OCCURRENCE_TIME, "nothing");
triggerActionBasedOnTheAlarmType(mAlarmType, mAlarmId, mOccurenceTime, context);
} catch (Exception e) {
TrackingEventLogHelper.logException(e, Constants.Global.EXCEPTION,
Constants.ExceptionMessage.EXC_CANNOT_PROCESS_RECEIVED_ALARM, true);
}
}
问题是变量mOccurenceTime始终为空,rsp。“没什么”
我试图从intent和Bundle中获得价值,但仍然没有成功。物品的捆绑数量是否有限制
如何以正确的方式获取价值mOccurenceTime
非常感谢您的建议。这是正确的方法-指定一个标志
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, uniqueRequestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
而不是:
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, uniqueRequestCode, intent, 0);
因为标志的0会让你头疼
这可能是一个非常普遍的问题,因为谷歌的示例代码忽略了在警报中包含额外的
解决方法如下:
是否为报警\u发生\u时间指定了唯一值?是的,它是硬编码常量(键),但值可以不同或相同(基于变量值)。用于将值放入捆绑包中的键是否完全不同?(特别是
常量.Global.ALARM\u UNIQUE\u ID
不同于常量.Global.ALARM\u OCCURRENCE\u TIME
)是的,但具有相同的“前缀”ALARM\u UNIQUE\u ID和ALARM\u exectimes只是为了确保:在文档(Intent.putExtras())中,它提到了捆绑包:“密钥必须包含一个包前缀”。您的密钥是否以包名开头?