3

I'm trying to use the APK Expansion extension from Google to download expansion files I have hosted with them. I'm also using the code from the SampleDownloadActivity to do this, albeit slightly modified to fit in my app.

My problem is that the download is never initiated. In my class that implements IDownloadClient, onStart() is called, but onServiceConnected() is not.

I have traced this down to this line in DownloaderClientMarshaller:

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

This always returns false, and therefore the service is not bound.

I'm using the calling activity within a TabHost, which has caused problems for other people. They were saying that you must not pass the TabHost context, rather that the Application context to the connect function. I've changed this by doing:

mDownloaderClientStub.connect(getApplicationContext());

instead of:

mDownloaderClientStub.connect(this);

but it doesn't help, I still get false. I'm doing all my testing on the Emulator if that makes a difference.

I'm really pulling my hair out on this one. If anyone has any ideas, I'd be extremely grateful!

Bolein95
  • 2,947
  • 2
  • 26
  • 31
user1454610
  • 91
  • 1
  • 3

1 Answers1

1

In most cases, bindService() method returns false if the service was not declared in the application's Manifest file.

In my case, the problem was that I had given the wrong class object to the DownloaderClientMarshaller.CreateStub() method. I accidentally used DownloaderService.class instead of MyDownloaderService.class.

When using the downloader API, make sure to pass the correct class object that extends the base DownloaderService.

I recommend using the updated Downloader Library included in Better APK Expansion package. It has this and other issues fixed and also provides simplified API that minimizes chances to shoot yourself in the foot.

To receive the download progress, you will just have to extend the 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);
            }
        }
    }

}

Check the full documentation on the library's page.

Bolein95
  • 2,947
  • 2
  • 26
  • 31