Android AlarmManager setRepeating问题
我正在开发一个馈送应用程序,并且我可以选择让用户为每个源(馈送源)选择指定从互联网获取数据并更新数据库的时间。
我为此使用 AlarmManager。
我通常做的是:
获取所有提供商。 在地图中分组>键是睡眠时间,值是提供者列表,
用于什么?...
好吧,我稍后会为每个键迭代这个字典,并安排一个警报来发送带有意图数据的广播。
在我的广播接收器中,我想获取这些数据..
我不理解的问题是我在每个循环中传递一个键(睡眠时间)来安排事件...实际上我有2个键,[5, 10 ]
并且在我的 onreceive 上我只得到密钥 5.. 2 次.... 似乎将相同的意图传递给接收者..
这是代码:
private void setAlarmForEnabledProviders(HashMap<Integer, ArrayList<Provider>> map)
{
Context ctx = FeederPeriodicalUpdater.this;
AlarmManager alarmMngr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
for(Integer key : map.keySet())
{
int keyValue = key.intValue();
Intent intent = new Intent(FeederPeriodicalUpdater.this, OnAlarmReceiver.class);
intent.putExtra(AlarmManager_IntentKey, keyValue);
PendingIntent pIntent = PendingIntent.getBroadcast(ctx, 0, intent, 0);
long firstTime = SystemClock.elapsedRealtime();
alarmMngr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, pIntent);
}
}
我的接收器:
public void onReceive(Context ctx, Intent intent)
{
Log.d(TAG, "onReceive");
application = (DroidApplication)ctx.getApplicationContext();
feedMessageService = application.getFeedMessageService();
int sleep = intent.getIntExtra(FeederPeriodicalUpdater.AlarmManager_IntentKey, -1);
if(sleep == -1)
throw new IllegalStateException();
I'm developing a feeder application, and i have the option that let the user choose for each source (feed source) specify the time to get data from the internet and update the database.
I'm using AlarmManager for this.
What i do tipically is:
Get all providers.
Group in a map> that the key is the sleep time and value is a list of providers
For what?...
Well i iterate later over this dictionary for each key and schedule an alarm to send a broadcast with data in a intent.
In my broadcast receiver i want to get this data..
The problem that i am not undestanding is i pass a Key (sleep time) in a for each loop to schedule the event... actually i got 2 keys, [5, 10]
and on my onreceive i get only key 5.. 2 times.... it seems like the same intent is delivered to the receiver..
Here is the code:
private void setAlarmForEnabledProviders(HashMap<Integer, ArrayList<Provider>> map)
{
Context ctx = FeederPeriodicalUpdater.this;
AlarmManager alarmMngr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
for(Integer key : map.keySet())
{
int keyValue = key.intValue();
Intent intent = new Intent(FeederPeriodicalUpdater.this, OnAlarmReceiver.class);
intent.putExtra(AlarmManager_IntentKey, keyValue);
PendingIntent pIntent = PendingIntent.getBroadcast(ctx, 0, intent, 0);
long firstTime = SystemClock.elapsedRealtime();
alarmMngr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, pIntent);
}
}
My Receiver:
public void onReceive(Context ctx, Intent intent)
{
Log.d(TAG, "onReceive");
application = (DroidApplication)ctx.getApplicationContext();
feedMessageService = application.getFeedMessageService();
int sleep = intent.getIntExtra(FeederPeriodicalUpdater.AlarmManager_IntentKey, -1);
if(sleep == -1)
throw new IllegalStateException();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将请求代码更改为 PendingIntent 的 getBroadcast() 的第二个参数,
例如传递唯一编号,因为您发送相同的意图和请求,我认为它将被覆盖
try to change the request code as the second parameter of the getBroadcast() of the PendingIntent
like pass the unique number because you send the same intent and request i think it will be overwrite