Fetch icon indicating copy to clipboard operation
Fetch copied to clipboard

Can anyone help me to implement this in Service class java

Open arunkr83 opened this issue 4 years ago • 1 comments

Will this work for parallel downloads in background?

public class MediaDownloadService extends Service {

private Fetch fetch;

private final FetchListener fetchListener = new FetchListener() {
    @Override
    public void onAdded(@NonNull Download download) {
       
    }

    @Override
    public void onQueued(@NonNull Download download, boolean waitingOnNetwork) {
       
    }

    @Override
    public void onWaitingNetwork(@NonNull Download download) {
       
    }

    @Override
    public void onCompleted(@NonNull Download download) {
      
    }

    @Override
    public void onError(@NonNull Download download, @NonNull Error error, @org.jetbrains.annotations.Nullable Throwable throwable) {
       
    }

   

    @Override
    public void onStarted(@NonNull Download download, @NonNull List<? extends DownloadBlock> list, int i) {
       
    }

    @Override
    public void onProgress(@NonNull Download download, long etaInMilliSeconds, long downloadedBytesPerSecond) {
        
    }

    @Override
    public void onPaused(@NonNull Download download) {
      
    }

    @Override
    public void onResumed(@NonNull Download download) {
        
    }

    @Override
    public void onCancelled(@NonNull Download download) {
       
    }

    @Override
    public void onRemoved(@NonNull Download download) {
      
    }

    @Override
    public void onDeleted(@NonNull Download download) {
       
    }
};


@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}


@Override
public void onCreate() {
    super.onCreate();
  

    FetchConfiguration fetchConfiguration = new FetchConfiguration.Builder(getApplicationContext())
            .setDownloadConcurrentLimit(3)
            .build();
    fetch = Fetch.Impl.getInstance(fetchConfiguration);


}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (ACTION_DOWNLOAD.equals(intent.getAction())) {
        String id = intent.getStringExtra(EXTRA_DOWNLOAD_ID);
        String type = intent.getStringExtra(EXTRA_DOWNLOAD_TYPE);

        switch (type) {
            case "pause all":
                pauseAll();
                break;
            case "resume all":
                resumeAll();
                break;
            case "start":
                downLoadFileNew(id);
                break;
            case "pause":
                pauseDownload(id);
                break;
            case "resume":
                resumeDownload(id);
                break;
            default:
                break;
        }
    }

    return START_STICKY;
}


private void pauseAll() {
   
        fetch.removeListener(fetchListener);
        fetch.pauseAll();

}

private void resumeAll() {
    
        fetch.addListener(fetchListener);
        fetch.resumeAll();
   
}

private void pauseDownload(String id) {
    fetch.removeListener(fetchListener);
    fetch.pause(id);
}

private void resumeDownload(String id) {
   fetch.resume(id);
   fetch.addListener(fetchListener);
}

private void downLoadFileNew(String key) {
 

        final Request request = new Request(message.getMediaUrl(), localFile.getPath());
        request.setPriority(Priority.HIGH);
        //request.setAutoRetryMaxAttempts(3);
        request.setNetworkType(NetworkType.ALL);

        fetch.enqueue(request,
                updatedRequest -> {
                    //Request was successfully enqueued for download.
                }, error -> {
                    downloadFailed(message);
                });
        fetch.addListener(fetchListener);

   

}

}

while i am trying to implement this in service class for background downloading, i am getting errors, any help will be appreciated

arunkr83 avatar Aug 09 '21 18:08 arunkr83

What errors do you get ? because i can make it work without writing my own service

OmidNejadabbasi avatar Sep 13 '22 14:09 OmidNejadabbasi