Android的AlarmManager setRepeating问题 [英] Android AlarmManager setRepeating Issue

查看:200
本文介绍了Android的AlarmManager setRepeating问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用给料机,我有让用户选择为每个源(饲料源)指定的时间从互联网上获取数据并更新数据库的选项。

我使用AlarmManager这一点。

我做什么tipi​​cally是:

获取所有商。
集团在一张地图>认为,关键在于睡眠时间和价值是供应商的名单

为了什么?...

嗯,我以后迭代在这个字典每个键,设定闹钟与数据的意图发送广播。

在我的广播接收机,我希望得到这些数据。

这是我没有undestanding问题是我在传递一个密钥(睡眠时间)for each循环来安排活动......其实我有2把钥匙,[5,10]

和我的onReceive我只得到键5。2倍....好像一样的意图传递到接收器。

下面是code:

 私人无效setAlarmForEnabledProviders(HashMap的<整数,ArrayList的<提供商GT;>图)
        {
            上下文CTX = FeederPeriodicalUpdater.this;
            AlarmManager alarmMngr =(AlarmManager)getSystemService(Context.ALARM_SERVICE);            对于(整数键:map.keySet())
            {
                INT的keyValue = key.intValue();
                意向意图=新意图(FeederPeriodicalUpdater.this,OnAlarmReceiver.class);
                intent.putExtra(AlarmManager_IntentKey,的keyValue);
                的PendingIntent pIntent = PendingIntent.getBroadcast(CTX,0,意向,0);                长firstTime = SystemClock.elapsedRealtime();
                alarmMngr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime,pIntent);
            }
        }

我的收款人:

 公共无效的onReceive(上下文CTX,意图意图)
{
    Log.d(TAG的onReceive);    应用=(DroidApplication)ctx.getApplicationContext();
    feedMessageService = application.getFeedMessageService();    INT睡眠= intent.getIntExtra(FeederPeriodicalUpdater.AlarmManager_IntentKey,-1);    如果(睡眠== -1)
        抛出新IllegalStateException异常();


解决方案

尝试更改请求code视)的PendingIntent的getBroadcast的第二个参数(

 的PendingIntent pIntent = PendingIntent.getBroadcast(CTX,在这里改变数值每次,意图,0);

像传递的唯一编号,因为你发送相同的目的和要求,我认为这将是覆盖

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();

解决方案

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

这篇关于Android的AlarmManager setRepeating问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆