简体   繁体   English

Android:如何将值从一个广播接收器传递到另一个?

[英]android: how to pass values from one broadcast receiver to another?

i have this broadcastreceiver that executes some code from time to time and triggers itself again by setting a timer (alarmmanager). 我有这个不定期执行一些代码并通过设置计时器(警报管理器)再次触发自身的广播接收器。 the result of the calculations is an integer value, which should be used as input for the next time the algorithm is executed. 计算的结果是一个整数值,该值应在下次执行算法时用作输入。 therefore, i need to store this value. 因此,我需要存储此值。

i don't want to do this with SQLite as this would produce an enormous overhead - but what are the alternatives? 我不想使用SQLite这样做,因为这会产生巨大的开销-但是有什么替代方法? manage this via shared preferences? 通过共享首选项进行管理? store it within files? 将其存储在文件中?

any good ideas? 有什么好主意吗?

Carry the data around in the PendingIntent you specify to the AlarmManager. 将您指定的PendingIntent中的数据携带到AlarmManager中。 However, instead of a repeating alarm, you might need to set a new alarm each time. 但是,您可能需要每次都设置一个新的警报,而不是重复警报。

I would probably put a broadcast pending intent into the alarm manager, and create that pending intent with an Intent that stores the int value you want to carry over. 我可能会将广播待处理的意图放入警报管理器中,并使用存储要保留的int值的Intent创建该待处理的意图。 You would create the pending intent with PendingIntent.getBroadcast() , with the data intent as an argument. 您可以使用PendingIntent.getBroadcast()创建暂挂意图,并以数据意图作为参数。 Your receiver would receive this intent and get the int value out of it. 您的接收者将收到此意图并从中获取int值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM