Hi I am downloading multiple files in background and showing progress bar in notification. But when I start my service my app starts lagging resulting into
Not Responding Dialog.
I search over internet found that service must run different thread. I did as they said but still it hangs application.
I have created service called DownloadService this has a downlaod functionality which is written n thr
What I have tried:
public class DownloadService extends Service {
private final int notificationId = 1;
private RemoteViews remoteViewsSmall, remoteViewsBig;
private NotificationManagerCompat notificationManager;
private NotificationCompat.Builder mBuilder;
public DownloadService() {
}
public class BackgroundThread implements Runnable {
int serviceId;
public BackgroundThread(int serviceId) {
this.serviceId = serviceId;
}
@Override
public void run() {
remoteViewsSmall = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_small);
remoteViewsBig = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification);
notificationManager = NotificationManagerCompat.from(DownloadService.this);
mBuilder = new NotificationCompat.Builder(DownloadService.this, "1");
mBuilder.setContentTitle("Picture Download")
.setContentText("Download in progress")
.setSmallIcon(R.mipmap.ic_launcher)
.setOngoing(true)
.setCustomContentView(remoteViewsSmall)
.setCustomBigContentView(remoteViewsBig)
.setPriority(NotificationCompat.PRIORITY_HIGH);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
int PROGRESS_MAX = 100;
try {
ArrayList<Download> list = new ArrayList<>();
list.add(new Download("https://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_5mb.mp4","file1.mp4"));
list.add(new Download("https://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_5mb.mp4","file.mp4"));
for (int i = 0; i < list.size(); i++) {
Download download = list.get(i);
URL url = new URL(download.url);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
int fileSize = urlConnection.getContentLength();
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard, download.fileName);
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fileOutput = new FileOutputStream(file);
InputStream inputStream = urlConnection.getInputStream();
byte[] buffer = new byte[1024];
int bufferLength = 0;
int count = 0;
while ((bufferLength = inputStream.read(buffer)) > 0) {
fileOutput.write(buffer, 0, bufferLength);
count += bufferLength;
int progress = (int) (count * 100 / (float) fileSize) + 1;
updateProgress(list.size(), i+1, progress, download.fileName);
notificationManager.notify(notificationId, mBuilder.build());
}
fileOutput.close();
updateProgress(list.size(), list.size(), 100, "Download complete");
mBuilder.setOngoing(false);
notificationManager.notify(notificationId, mBuilder.build());
}
Log.e("DownloadingService", "File downloaded successfully");
} catch (MalformedURLException e) {
Log.e("DownloadingService", "Failed: Exception:", e);
} catch (IOException e) {
Log.e("DownloadingService", "Failed: Exception:", e);
}
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Service started", Toast.LENGTH_SHORT).show();
Thread thread = new Thread(new BackgroundThread(startId));
thread.start();
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
}
private void updateProgress(int totalFiles, int currentFileCount, int currentProgress, String fileName) {
remoteViewsSmall.setTextViewText(R.id.tvOverAll, currentProgress+"%");
remoteViewsSmall.setProgressBar(R.id.overallProgress, 100, currentProgress, false);
remoteViewsBig.setTextViewText(R.id.tvOverAll, currentFileCount+"/"+totalFiles);
remoteViewsBig.setTextViewText(R.id.tvFileName, "Downloading "+fileName);
remoteViewsBig.setProgressBar(R.id.overallProgress, totalFiles, currentFileCount, false);
remoteViewsBig.setProgressBar(R.id.currentProgress, 100, currentProgress, false);
}
}