3

我試圖使用Google的APK擴展擴展來下載我與他們託管的擴展文件。我也使用SampleDownloadActivity的代碼來做到這一點,雖然稍作修改以適應我的應用程序。Android:bindService始終返回false(擴展APK api)

我的問題是,永遠不會啓動下載。在實現IDownloadClient的類中,調用onStart(),但onServiceConnected()不是。

我已經追查這歸因於該行DownloaderClientMarshaller:

if(c.bindService(bindIntent, mConnection, Context.BIND_DEBUG_UNBIND)) { 

這始終返回false,因此該服務沒有約束。

我在TabHost中使用調用活動,這對其他人造成了問題。他們說你不能傳遞TabHost上下文,而是將Application上下文傳遞給connect函數。我這樣做改變了這個:代替

mDownloaderClientStub.connect(getApplicationContext()); 

mDownloaderClientStub.connect(this); 

,但它並沒有幫助,我仍然得到錯誤。如果這有所幫助,我正在模擬器上進行所有測試。

我真的把我的頭髮拉出來。如果有人有任何想法,我會非常感激!

+0

檢查此:可能幫助.. http://stackoverflow.com/a/2916829/1777090 – 2012-12-12 05:45:39

+0

你解決了這個問題? – Bolein95 2017-07-18 11:15:56

回答

1

在大多數情況下,如果服務未在應用程序的清單文件中聲明,則bindService()方法將返回false

在我的情況下,問題是我給了DownloaderClientMarshaller.CreateStub()方法錯誤的類對象。我不小心使用了DownloaderService.class而不是MyDownloaderService.class

使用下載器API時,請務必傳遞擴展基址DownloaderService的正確類對象。

我推薦使用包含在Better APK Expansion包中的更新後的下載程序庫。它解決了這個問題和其他問題,並且還提供了簡化的API,從而最大限度地減少了在腳中拍攝自己的機會。

要獲得下載進度,您只需要擴展BroadcastDownloaderClient

public class SampleDownloaderActivity extends AppCompatActivity { 
    private final DownloaderClient mClient = new DownloaderClient(this); 

    // ... 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     mClient.register(this); 
    } 

    @Override 
    protected void onStop() { 
     mClient.unregister(this); 
     super.onStop(); 
    } 

    // ... 

    class DownloaderClient extends BroadcastDownloaderClient { 

     @Override 
     public void onDownloadStateChanged(int newState) { 
      if (newState == STATE_COMPLETED) { 
       // downloaded successfully... 
      } else if (newState >= 15) { 
       // failed 
       int message = Helpers.getDownloaderStringResourceIDFromState(newState); 
       Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); 
      } 
     } 

     @Override 
     public void onDownloadProgress(DownloadProgressInfo progress) { 
      if (progress.mOverallTotal > 0) { 
       // receive the download progress 
       // you can then display the progress in your activity 
       String progress = Helpers.getDownloadProgressPercent(
         progress.mOverallProgress, progress.mOverallTotal); 
       Log.i("SampleDownloaderActivity", "downloading progress: " + progress); 
      } 
     } 
    } 

} 

查看圖書館的page的完整文檔。