2012-10-02 30 views
0

我正在寫一個自定義事件,並希望得到一些幫助。我將要談論的大部分內容是基於Custom event listener on Android app提供的幫助在android中創建自定義事件的問題

所以這裏是我的問題。我正在編寫一個應用程序,需要從網絡上下載更新的圖像,將圖像存儲在手機中,然後再顯示這些圖像。基本上,我在啓動畫面中下載任何所需的圖像。然後,當圖像下載並存儲時,啓動畫面將清除,並且任何必要的(新下載的)圖像都會顯示在屏幕上。問題在於:下載過程是通過一個asynctask來完成的,因此圖像加載到屏幕上的部分不能在asynctask中完成。它必須在主UI線程上完成。我想爲主線程創建一個事件和一個自定義事件監聽器來監聽,這基本上告訴主UI線程開始從內存加載下載的圖像是安全的。

根據從上面的鏈接的討論中,我想出了這個迄今爲止...下載收聽interace

public interface DataDownloadListener { 
void onDownloadStarted(); 
void onDownloadFinished(); 
} 

事件類...

public class DataDownloadEvent { 
     ArrayList<DataDownloadListener> listeners = new ArrayList<DataDownloadListener>(); 
     public void setOnDownload(DataDownloadListener listener){ 
      this.listeners.add(listener); 
     } 
} 

我的問題是我不明白在這些說明中把最後兩步放在哪裏。我認爲我必須將聽衆和事件放在實際啓動下載的類中。但是哪裏?這裏是我的功能啓動下載並保存到設備:

public String download(String sourceLocation) { 
    String filename = ""; 
    String path = ""; 
    try { 
     File externalStorageDirectory = Environment 
       .getExternalStorageDirectory(); 
     URL urlTmp = new URL(sourceLocation); 
     filename = urlTmp.getFile() 
       .substring(filename.lastIndexOf("/") + 1); 
     path = externalStorageDirectory + PATH; 

     // check if the path exists 
     File f = new File(path); 
     if (!f.exists()) { 
      f.mkdirs(); 
     } 

     filename = path + filename; 
     f = new File(filename); 

     //only perform the download if the file doesn't already exist 
     if (!f.exists()) { 

      Bitmap bitmap = BitmapFactory.decodeStream(urlTmp.openStream()); 
      FileOutputStream fileOutputStream = new FileOutputStream(
        filename); 
      if (bitmap != null) { 
       bitmap.compress(getFormat(filename), 50, fileOutputStream); 
       Log.d(TAG, "Saved image " + filename); 
       return filename; 
      } 
     } 
     else{ 
      Log.d(TAG, "Image already exists: " + filename + " Not re-downloading file."); 
     } 
    } catch (MalformedURLException e) { 
     //bad url 
    } catch (IOException e) { 
     //save error 
    } 

    return null; 
} 

而有關注冊聽衆,你在哪裏我把那最後一步?說明書說在初始化期間將其放在某處。這是否意味着我的主要活動的onCreate方法?在主要活動的進口部分的課外?從未做過自定義事件,所以任何幫助將不勝感激。

+0

另外,我知道我可以使用廣播做同樣的事情,但我認爲這可能有點冒失......我認爲正確的做法是創建事件和聽衆... –

回答

0

根據從上面的鏈接的討論中,我想出了這個迄今爲止...下載收聽interace

公共接口DataDownloadListener { 無效onDownloadStarted(); void onDownloadFinished(); }

事件類...

public class DataDownloadEvent { 
     ArrayList<DataDownloadListener> listeners = new ArrayList<DataDownloadListener>(); 
     public void setOnDownload(DataDownloadListener listener){ 
      this.listeners.add(listener); 
     } 
} 

好吧......

您的下載過程中

現在,在下載的開始時間,週期都在聽衆的元素 ArrayList並調用onDownloadStarted事件通知所有聽衆下載剛剛開始(在這種情況下,我認爲你需要打開splashscreen)。

您的下載過程中

始終,在和下載的,週期都在聽衆的元素 ArrayList和調用onDownloadFinished事件通知所有的監聽器下載完成後(現在關閉閃屏) 。

上下載如何循環聽衆完成

foreach(DataDownloadListener downloadListener: listeners){ 
     downloadListener.onDownloadFinished(); 
    } 

上下載如何循環聽衆開始

foreach(DataDownloadListener downloadListener: listeners){ 
     downloadListener.onDownloadStarted(); 
    } 
+0

Th在讓我走。認爲它會比這更復雜...另外,爲了使我的事件工作,在類DataDownloadEvent中,我不得不改變類的第一行,以在ArrayList前面包含單詞「static」,並更改在setOnDownload函數中將「this」改爲「DataDownloadEvent」。之後,一切正常......謝謝。 –

0

不要讓靜態如果可能的話......在課堂上您將使用它來下載文件,只需添加您在類中添加的內容(偵聽器arrayList和用於添加的設備方法刪除)。你沒有立即需要以這種方式使用一個類(我的意思是靜態成員)。

public class DownloadFileClassExample{ 
     private ArrayList<DataDownloadListener> listeners = new ArrayList<DataDownloadListener>(); 

    public DownloadFileClassExample(){ 

    } 

    public void addDownloadListener(DataDownloadListener listener){ 
     listeners.add(listener); 
    } 

    public void removeDownloadListener(DataDownloadListener listener){ 
     listeners.remove(listener); 
    } 
    //this is your download procedure 
    public void downloadFile(){...} 
    } 

然後訪問你的類以這種方式

DownloadFileClassExample example = new DownloadFileClassExample(); 
    example.addDownloadListener(this); // if your class is implementing the **DataDownloadListener** 

或使用

example.addDownloadListener(new DataDownloadListener{...})