2012-12-11 34 views
1

我想讓我的應用程序下載一個擴展文件。這是我的代碼到目前爲止。Android擴展文件不會觸發onDownloadStateChanged?

public class main extends Activity implements IDownloaderClient { 

TextView tv1; 
Button mcButt; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    ////////// 
    tv1 = (TextView)findViewById(R.id.textView1); 

    // Check whether the file have been downloaded. 
    String mainFileName = Helpers.getExpansionAPKFileName(this, true, 1); 
    boolean fileExists = Helpers.doesFileExist(this, mainFileName, 32921796L, false); 

    if (!fileExists) { 

    Intent launcher = getIntent(); 
    Intent fromNotification = new Intent(this, getClass()); 
    fromNotification.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    fromNotification.setAction(launcher.getAction()); 

      if (launcher.getCategories() != null) { 
       for (String cat : launcher.getCategories()) 
       { fromNotification.addCategory(cat); } 
      } 

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, fromNotification, PendingIntent.FLAG_UPDATE_CURRENT); 

    try { 

    int result = DownloaderClientMarshaller.startDownloadServiceIfRequired(this, pendingIntent, ExpansionFileDownloaderService.class); 
    if (DownloaderClientMarshaller.NO_DOWNLOAD_REQUIRED != result) { 

     // implement Downloading UI. 
     tv1.setText("Need to download."); 

    return; 
    } 

    } catch (Exception e) { 
    Log.e("apk-expansion-files", "NameNotFoundException occurred. " + e.getMessage(), e); 
    } 
    } 

    tv1.setText("Downloaded!"); 


} 


/////////////////////////////////////////////////// 


@Override 
public void onServiceConnected(Messenger m) { 
    tv1.setText("onServiceConnected: " + m.toString()); 
} 

/////////// 

@Override 
public void onDownloadStateChanged(int newState) { 
    tv1.setText("onDownloadStateChanged: " + String.valueOf(newState)); 
} 

//////////// 

@Override 
public void onDownloadProgress(DownloadProgressInfo progress) { 
    ProgressBar mPB = (ProgressBar)findViewById(R.id.progressBar1); 

    progress.mOverallTotal = progress.mOverallTotal; 
    mPB.setMax((int) (progress.mOverallTotal >> 8)); 
    mPB.setProgress((int) (progress.mOverallProgress >> 8)); 
    tv1.setText(Long.toString(progress.mOverallProgress * 100/progress.mOverallTotal) + "%"); 
    tv1.setText(Helpers.getDownloadProgressString(progress.mOverallProgress,progress.mOverallTotal)); 

} 

}

文件並下載很好,但它不會觸發onDownloadProgress或過程中或下載後onDownloadStateChanged。

想到什麼,是DownloaderClientMarshaller需要與IDownloaderClient其落實到活動以某種方式連接...

任何想法?謝謝!

+0

您可能需要設置事件偵聽器? – adarsh

+0

顯然是的......你認爲我應該把它與DownloaderClientMarshaller連接起來嗎? –

+0

我可以驗證我沒有得到onDownloadStateChanged響應 –

回答

4

您應該創建並存根並將其連接到上下文。然後,這些事件將被髮送到您的IDownloaderClient。

if (DownloaderClientMarshaller.NO_DOWNLOAD_REQUIRED != result) { 
    downloaderClientStub = DownloaderClientMarshaller.CreateStub(this, 
        YourDownloaderServiceClass.class); 
    downloaderClientStub.connect(this);  
}