2013-03-26 61 views
0

我試圖評估使用單一意圖的長期過程與最多五個單獨意圖的好處和成本。使用單一意圖檢索多個記錄與每個記錄的單獨意圖

我的應用程序的用戶從網上下載記錄被分析到數據庫中。用戶可以在任何給定時間下載1到5人的記錄。

我有應用程序設置是這樣的:

private OnClickListener mySubmitOnClick = new OnClickListener() { 

    @Override 
    public void onClick(View v) { 

     ArrayList<String> idsOfRecordsArray = getIdsOfRecordsToDownload(); 

       Intent intentToRetrieve = new Intent(getActivity(), 
                RetrieveData.class); 
     Messenger messenger = new Messenger(handler); 
     intentToRetrieve.putExtra("MESSENGER", messenger); 

     intentToRetrieve.putStringArrayListExtra ("IdList", 
                idsOfRecordsArray); 

     v.getContext().startService(intentToRetrieve); 

     showDialog("Some Message"); 

     } 
     } 

這工作,但由於分析數據所需的時間,我有時會得到超時錯誤。

我改成了這樣:

private OnClickListener mySubmitOnClick = new OnClickListener() { 

    @Override 
    public void onClick(View v) { 

     ArrayList<String> idsOfRecordsArray = getIdsOfRecordsToDownload(); 
       while(iCount < idsOfRecordsArray.size()) { 
     ArrayList<String> currentID = new ArrayList<String>(); 

     currentAthleteID.add(athleteIdArray.get(iCountAthletes)); 
     Intent intentToRetrieve = new Intent(getActivity(), 
               RetrieveData.class); 
     Messenger messenger = new Messenger(handler); 
     intentToRetrieve.putExtra("MESSENGER", messenger); 

     intentToRetrieve.putStringArrayListExtra ("IdList", 
                   currentID); 

     v.getContext().startService(intentToRetrieve); 

     showDialog("Some Message"); 

     iCount++; 
     } 
     } 

所以我現在可以有多達五(5)意圖在同一時間發射了。這似乎很穩定,夠了。但是,處理進度對話框有點麻煩。如果我只打開1個對話框,它將在處理完成之前關閉。

這裏有什麼想法更好?或者我應該放棄這兩種方法?

回答

0

使用IntentService在後臺執行工作。您可以根據需要啓動儘可能多的意圖,但IntentService一次只能處理一個。同時,你不會超時,用戶可以繼續工作。我認爲用戶會接受結果不會立即到達,只要他們在等待時可以做其他事情。

以Android培訓課Running in a Background Service爲例。

+0

這正是我所需要的。謝謝! – PhillipOReilly 2013-03-27 21:47:44