NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

package tt.downloaderornek;

import android.Manifest;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.util.Log;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class MainActivity extends AppCompatActivity
{
boolean izinOk = true;

EditText etAd, etURL;
TextView tvDurum;
ProgressBar pb;
File dlFile;

public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions, @NonNull int[] grantResults)
{
izinOk = grantResults[0] == PackageManager.PERMISSION_GRANTED;
Log.e("x","İzin Verildi Mi : "+izinOk);
}

protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

String izinler[] =
{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};

// Kullanıcıdan İzin İste
ActivityCompat.requestPermissions(MainActivity.this, izinler, 12);

etAd = (EditText) findViewById(R.id.etName);
etURL = (EditText) findViewById(R.id.etURL);

etURL.setText("http://muhammetcagatay.com/dosyalar/academytech_java_tr.pdf");
tvDurum = (TextView) findViewById(R.id.tvDurum);
pb = (ProgressBar) findViewById(R.id.pb);
}

public void startDownload(View v)
{
// Dosyayı İndiriecek AsenkronTask Oluştur && Başlat
new AsyncTask<String, Integer, String>()
{
int dosyaBoyutu, indirilen, anlikOkunan, yuzde;
String adres;

protected void onPreExecute()
{
adres = etURL.getText().toString();

// Kullanıcının İstediği Dosyayı Oluştur
try
{
// Telefon Hafızasında, Kullanıcının Verdiği Adta Bir Dosya
dlFile = new File(Environment.getExternalStorageDirectory(),
etAd.getText().toString());
dlFile.createNewFile();
dlFile.setReadable(true);
dlFile.setWritable(true);
dlFile.setExecutable(true);
} catch (Exception e) { Log.e("x","dlFile Olusturulamadı : "+e); }
}

protected String doInBackground(String... strings)
{
try
{
// Indirilecek Dosyayi URL Olarak Tanimla ve Baglanti Ac
URL url = new URL(adres);
URLConnection baglanti = url.openConnection();
// Indirilecek Dosyanın Boyutunu Al
dosyaBoyutu = baglanti.getContentLength();

byte[] tmp = new byte[32768];

// VeriOkumak Icin, baglantidan Bir InputStream
InputStream is = baglanti.getInputStream();
// Okunan byte Verisini Dosyaya Yazmak Icin FileOutputStream
FileOutputStream fos = new FileOutputStream(dlFile);

// is'den Veri Geldiği Sürece, max 32KB'lik Veri Oku,
// Ne Kadar Veri Okudugunu, anlik Okunana Kaydet
while (( anlikOkunan = is.read(tmp)) != -1)
{
// tmp arrayinin ne kadari doldurulmussa, dosyayi yaz
fos.write(tmp, 0, anlikOkunan);
// Dosyanın Ne Kadarı İndirildi ?
indirilen += anlikOkunan;
Log.e("x","@"+anlikOkunan+" --> "+indirilen+"/"+dosyaBoyutu);
// UI GUNCELLE
yuzde = (indirilen * 100) / dosyaBoyutu;
publishProgress(dosyaBoyutu, indirilen, yuzde);
}

// Bağlantıyı Kapat && Dosyayı Kaydet
is.close();
fos.close();
} catch (Exception e) { Log.e("x","DL Hatası : "+e); return e.toString(); }
return "ok";
}

protected void onProgressUpdate(Integer... values)
{
pb.setMax(values[0]);
pb.setProgress(values[1]);
tvDurum.setText("% "+values[2]);
}

protected void onPostExecute(String s)
{
NotificationManager nM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder nB = new NotificationCompat.Builder(MainActivity.this);
nB.setSmallIcon(R.mipmap.ic_launcher);
nB.setContentTitle("Dosya İndirildi");
nB.setContentText(dlFile.getAbsolutePath());

nB.setTicker("Download Completed");

// Intent Olustur
Intent i = new Intent(Intent.ACTION_VIEW);
String ext = dlFile.getName().substring( dlFile.getName().lastIndexOf('.') + 1);
String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
i.setDataAndType(Uri.fromFile(dlFile), mime);

PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, i, 0);
nB.setContentIntent(pi);

Notification bildirim = nB.build();

nM.notify(0, bildirim);
}
}.execute();
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.