2011-08-24 66 views
0

我正在開發一個應用給料機,和我有讓用戶選擇爲每個源(飼料來源)指定的時間從互聯網上獲取數據並更新數據庫的選項。的Android AlarmManager setRepeating問題

我使用AlarmManager這一點。

我所做的一切是:

獲取所有供應商。 集團在地圖>認爲,關鍵在於睡眠時間和價值是提供

爲了什麼?列表...

嗯,我以後迭代在這個字典每個鍵並安排警報發送意向數據廣播。

在我的廣播接收機,我想這個數據..

,我不是undestanding是我通過一個鍵(睡眠時間),在每個循環調度該事件的問題...其實我用了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(); 
+0

我很高興你試圖實現我在上一個問題中給你的設計:http://stackoverflow.com/questions/7166333/android-long-service-create -bg-thread-and-i-dont-know-that-is-going-on /接受我的答案mayb è? – darma

回答

1

嘗試改變的PendingIntent

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

的請求代碼爲getBroadcast(第二個參數),如通過獨特的數字,因爲你發送相同的目的和要求我認爲它會被覆蓋

+0

沒錯,我輸入的是同樣的答案,就這樣試試。 – darma

+0

謝謝你,我非常感謝你的幫助。 該文件至少是弱... 這就是它說.... 檢索PendingIntent將執行廣播,如調用Context.sendBroadcast()。 參數 上下文\t此PendingIntent應執行廣播的上下文。 requestCode \t發件人的私人請求代碼(當前未使用)。 意圖\t意圖播出。 – anotherNeo

+0

3工廠方法...我想現在這是實現像一個字典返回給我的密鑰的緩存版本...所以當我第二次使用,不傳遞任何東西,返回給我的價值是相同的。 ...... – anotherNeo