4

PendingIntents有一些問題。每次我的應用程序打開時,它都會安排一些廣播。 我的問題是已經存在的PendingIntents無法識別。Android PendingIntent FLAG_NO_CREATE不返回null

我知道必須使用相同的參數創建PendingIntents和uninterlaying Intents。

她是我的代碼...在我的Launcher Activity中作爲Asynctask開始。

 long nextExecute = getNextExecute(t); 

     if (nextExecute > System.currentTimeMillis()) { 

      int tt = 12; 

      intent = new Intent(context, BirthExecutor.class); 
      intent.putExtra(Target.ROW_ID, tt); 

      PendingIntent pending = PendingIntent.getBroadcast(context, 10, intent, PendingIntent.FLAG_NO_CREATE); 

      if (pending != null) { 

       manager.set(AlarmManager.RTC_WAKEUP, nextExecute, pending); 

       BirthLog log = new BirthLog("Scheduled " + t.getFirstname() + " " + t.getLastname() + " on " + df.format(nextExecute)); 
       log_helper.insertLog(log); 

      } else { 

       BirthLog log = new BirthLog(t.getFirstname() + " " + t.getLastname() + " already active!"); 
       log_helper.insertLog(log); 

      } 

      intent = new Intent(LogAdapter.LOG_RECEIVE_ACTION); 
      context.sendBroadcast(intent); 

     } 

每次都執行完全相同的代碼,但爲什麼掛起的變量不爲空?它一定要是 !!! 我已硬編碼的requestId putExtra硬編碼。

編輯

我還有一個功能更新此時間表。 只有當我執行該功能PendingIntents不再被識別。我試圖使用相同的上下文對象作爲靜態引用,但也失敗了。

public void updateTask(Target t) { 

    if (t.getTime() == null || t.getRow() == null) 
     return; 

    Intent intent; 
    SimpleDateFormat df = new SimpleDateFormat("HH:mm dd.MM.yyyy", Locale.US); 

    long nextExecute = getNextExecute(t); 

    if (nextExecute > System.currentTimeMillis()) { 

     intent = new Intent(static_context, BirthExecutor.class); 
     intent.putExtra(Target.ROW_ID, 12); 

     PendingIntent pending = PendingIntent.getBroadcast(static_context, 10, intent, PendingIntent.FLAG_CANCEL_CURRENT); 

     if (pending != null) { 

      manager.set(AlarmManager.RTC_WAKEUP, nextExecute, pending); 

      BirthLog log = new BirthLog("Scheduled " + t.getFirstname() + " " + t.getLastname() + " on " + df.format(nextExecute)); 
      log_helper.insertLog(log); 
      Log.d("birth", log.getComment()); 

     } else { 

      BirthLog log = new BirthLog(t.getFirstname() + " " + t.getLastname() + " ERROR. TIME: " + df.format(nextExecute)); 
      log_helper.insertLog(log); 
      Log.d("birth", log.getComment()); 

     } 

     intent = new Intent(LogAdapter.LOG_RECEIVE_ACTION); 
     static_context.sendBroadcast(intent); 

    } 

} 

EDIT 2

好這讓SENCE,如果它返回一個非空結果如果掛起的意圖已經被調度。 我改變了我的代碼。每次我旋轉我的手機時,主要活動onCreate被觸發並且調度asynctask被執行。但結果總是空的?!?!如果已經有計劃的pendingintent,它應該是非空的結果,對嗎?

@Override 
protected Void doInBackground(Void... params) { 

    Intent intent; 
    SimpleDateFormat df = new SimpleDateFormat("HH:mm dd.MM.yyyy", Locale.US); 

    for (Target t : target_helper.getAllTarget()) { 

     long nextExecute = getNextExecute(t); 

     if (nextExecute > System.currentTimeMillis()) { 

      PendingIntent pending = getPendingIntent(context, t, false); 

      if (pending != null) { 

       BirthLog log = new BirthLog(t.getFirstname() + " " + t.getLastname() + " already active!"); 
       log_helper.insertLog(log); 

      } else { 

       manager.set(AlarmManager.RTC_WAKEUP, nextExecute, pending); 

       BirthLog log = new BirthLog("Scheduled " + t.getFirstname() + " " + t.getLastname() + " on " + df.format(nextExecute)); 
       log_helper.insertLog(log); 

      } 

      intent = new Intent(LogAdapter.LOG_RECEIVE_ACTION); 
      context.sendBroadcast(intent); 

     } 

    } 

    return null; 

} 

private PendingIntent getPendingIntent(Context context, Target t, boolean update) { 

    Intent intent = new Intent(context, BirthExecutor.class); 
    intent.putExtra(Target.ROW_ID, t.getRow()); 

    if (update) { 
     return PendingIntent.getBroadcast(context, t.getRow().intValue(), intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    } else { 
     return PendingIntent.getBroadcast(context, t.getRow().intValue(), intent, PendingIntent.FLAG_NO_CREATE); 
    } 

} 

回答

7

該文檔不正確。如果您在致電PendingIntent.getBroadcast()時使用PendingIntent.FLAG_NO_CREATE,則將返回null如果找不到匹配的PendingIntent。否則它將返回匹配的PendingIntent

的文件說,這是周圍的其他方式,但這是錯誤的:-(

+0

OMG真的是這樣哎呀......我會看看...謝謝;) – Pascal

+3

實際上,如果你想了幾秒鐘以上,很明顯,如果有一個匹配的PendingIntent,返回'null',如果沒有一個匹配,則返回非null ,因爲通過設置「FLAG_NO_CREATE」,你已經表明你不希望Android創建一個新的PendingIntent **。它應該返回爲非空結果? –

+0

你解決了嗎? 我面臨着完全相同的問題 –

2

系統不正是文件說:表明

INT FLAG_NO_CREATE

標誌,如果描述PendingIntent 不存在,則簡單地返回null而不是創建 它。

隨着中說,如果我不想,如果它已經運行,開始我的任務,我這樣做:如果

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
Intent intent = new Intent(context, AlarmReceiver.class); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_NO_CREATE); 
if (pendingIntent == null) { 
    pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
    // start it only it wasn't running already 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10 * 60 * 1000, pendingIntent); 
}