Android AlarmManager setRepeating问题

发布于 2024-12-01 13:10:29 字数 1613 浏览 1 评论 0原文

我正在开发一个馈送应用程序,并且我可以选择让用户为每个源(馈送源)选择指定从互联网获取数据并更新数据库的时间。

我为此使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉世弥音 2024-12-08 13:10:29

尝试将请求代码更改为 PendingIntent 的 getBroadcast() 的第二个参数,

PendingIntent pIntent = PendingIntent.getBroadcast(ctx, "here change the value everytime", intent, 0);

例如传递唯一编号,因为您发送相同的意图和请求,我认为它将被覆盖

try to change the request code as the second parameter of the getBroadcast() of the PendingIntent

PendingIntent pIntent = PendingIntent.getBroadcast(ctx, "here change the value everytime", intent, 0);

like pass the unique number because you send the same intent and request i think it will be overwrite

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文