I have a custom download manager in my app, it works fine, it works on a service and it can not handle it directly, so an alternative would be to add an action in the progress notification, however it uses Intents and not listeners, then how can I manipulate my service directly by notification?
package com.samuelives.videoplayer.downloader;
import android.app.IntentService;
import android.app.NotificationManager;
import android.content.Intent;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.util.Log;
import android.webkit.MimeTypeMap;
import com.samuelives.videoplayer.R;
import com.samuelives.videoplayer.util.StorageHelper;
import com.samuelives.videoplayer.util.Web;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
public class Downloader extends IntentService{
...
@Override
protected void onHandleIntent(@Nullable Intent intent) {...}
private void download(final URL url){
new Thread(new Runnable() {
HttpURLConnection conn;
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
@Override
public void run() {
try{
conn = (HttpURLConnection)url.openConnection();
conn.setRequestProperty("User-Agent", Web.getUserAgent(Downloader.this));
File dFile = new File(Uri.parse(getDestPath()).getPath());
if(!dFile.exists()){
fos = new FileOutputStream(dFile);
}else{
conn.setRequestProperty("Range", "bytes=" + dFile.length() + "-");
downloaded = (int)dFile.length();
fos = new FileOutputStream(dFile, true);
}
conn.connect();
if(processResponse(conn.getResponseCode(), conn.getResponseMessage())) {
int totalSize = conn.getContentLength();
Log.d("Downloader", "Total size: " + totalSize);
NotificationCompat.Builder notification = new NotificationCompat.Builder(Downloader.this, "IVP_NOTIFICATION_CHANEL")
.setSmallIcon(R.drawable.ic_download)
.setContentTitle(getResources().getString(R.string.dm_downloading_msg))
.setContentText(title)
.setProgress(totalSize, 0, false)
.setAutoCancel(true)
.setOngoing(true)
.setCategory(NotificationCompat.CATEGORY_PROGRESS)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setOnlyAlertOnce(true);
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification.build());
bis = new BufferedInputStream(conn.getInputStream());
bos = new BufferedOutputStream(fos);
byte[] data = new byte[8192];
int pos = 0;
synchronized(this){
while ((pos = bis.read(data, 0, 8192)) != -1) {
if(isCancelled)
break;
downloaded += pos;
bos.write(data, 0, pos);
notification.setContentTitle(getResources().getString(R.string.dm_downloading_msg) + " " + (downloaded * 100) / totalSize + "%");
notification.setProgress(totalSize, downloaded, false);
notificationManager.notify(NOTIFICATION_ID, notification.build());
wait(100);
}
}
notification.setProgress(0, 0, false);
notificationManager.notify(NOTIFICATION_ID, notification.build());
notificationManager.cancel(NOTIFICATION_ID);
}
}catch (IOException | InterruptedException e){
Log.e("Downloader", "Falha ao baixar o arquivo: " + e.getMessage());
}finally {
if(conn != null)
conn.disconnect();
try{
if(bis != null)
bis.close();
if(bos != null){
bos.flush();
bos.close();
}
if(fos != null){
fos.flush();
fos.close();
}
}catch (IOException e){
Log.e("Downloader", "Falha ao encerrar os buffers: " + e.getMessage());
}
}
}
}).start();
}
private String getDestPath(){...}
private boolean processResponse(int code, String message){...}
}