Skip to content

Instantly share code, notes, and snippets.

@kjromero
Created June 4, 2019 19:45
Show Gist options
  • Select an option

  • Save kjromero/db33c024ac57db9d4c45af211d78f919 to your computer and use it in GitHub Desktop.

Select an option

Save kjromero/db33c024ac57db9d4c45af211d78f919 to your computer and use it in GitHub Desktop.
Class for download video in streaming
public class DownloadService extends IntentService {
/**
* Request code para el PendingIntent que se usa para ir a la pantalla principal.
*/
private static final int REQUEST_CODE_GO_TO_MAIN_ACTIVITY = 1;
public DownloadService() {
super("DownloadService");
}
@Override
protected void onHandleIntent(Intent intent) {
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
String contentTitle = "Start downloading";
Intent notifyIntent = new Intent();
PendingIntent notifyPendingIntent = PendingIntent.getActivity(this, REQUEST_CODE_GO_TO_MAIN_ACTIVITY, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = createNotificationBuilder("downloader_channel");
notificationBuilder.setContentIntent(notifyPendingIntent);
notificationBuilder.setTicker("Start downloading from the server");
notificationBuilder.setOngoing(true);
notificationBuilder.setAutoCancel(false);
notificationBuilder.setSmallIcon(android.R.drawable.stat_sys_download);
notificationBuilder.setContentTitle(contentTitle);
notificationBuilder.setContentText("0%");
notificationBuilder.setProgress(100, 0, false);
notificationManagerCompat.notify(REQUEST_CODE_GO_TO_MAIN_ACTIVITY, notificationBuilder.build());
String urlToDownload = intent.getStringExtra("url");
ResultReceiver receiver = (ResultReceiver) intent.getParcelableExtra("receiver");
boolean success = true;
try {
URL url = new URL(urlToDownload);
URLConnection connection = url.openConnection();
connection.connect();
// this will be useful so that you can show a typical 0-100% progress bar
int fileLength = connection.getContentLength();
String fileName = Uri.parse(urlToDownload).getLastPathSegment();
// download the file
InputStream input = new BufferedInputStream(connection.getInputStream());
OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory() +"/"+ fileName);
byte data[] = new byte[1024];
long total = 0;
int count, tmpPercentage = 0;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
//Publishing the progress....
int percentage = (int) ((total * 100) / fileLength);
if (percentage > tmpPercentage) {
notificationBuilder.setContentText(percentage + "%");
notificationBuilder.setProgress(100, percentage, false);
notificationManagerCompat.notify(REQUEST_CODE_GO_TO_MAIN_ACTIVITY, notificationBuilder.build());
tmpPercentage = percentage;
}
}
output.flush();
output.close();
input.close();
success = true;
} catch (IOException e) {
e.printStackTrace();
success = false;
}
contentTitle = "Downloaded";
String statusText = success ? "Done" : "Fail";
int resId = success ? android.R.drawable.stat_sys_download_done : android.R.drawable.stat_notify_error;
notificationBuilder.setContentTitle(contentTitle);
notificationBuilder.setSmallIcon(resId);
notificationBuilder.setOngoing(false);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));
NotificationManager notificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
notificationBuilder.setContentText(statusText);
notificationBuilder.setProgress(0, 0, false);
notificationManagerCompat.notify(REQUEST_CODE_GO_TO_MAIN_ACTIVITY, notificationBuilder.build());
}
private NotificationCompat.Builder createNotificationBuilder(String channelId) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
String channelName = getString(R.string.app_name);
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.createNotificationChannel(notificationChannel);
}
}
return new NotificationCompat.Builder(this, channelId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment