บทความนี้แชร์รหัสเฉพาะของการติดตั้ง Rxjava การใช้งานไฟล์ดาวน์โหลดสำหรับการอ้างอิงของคุณ เนื้อหาเฉพาะมีดังนี้
บทความนี้ใช้: ติดตั้งเพิ่มเติม + rxjava
1. เปิดสอน:
// rxjava compile 'io.reactivex: rxjava: ล่าสุด release' compile 'io.reactivex: rxandroid: ล่าสุด. release' // เครือข่าย - squareup compile 'com.squareup.retrofit2: retrofit 'com.squareup.retrofit2: Adapter-rxjava: ล่าสุด release' compile 'com.squareup.okhttp3: okhttp: ล่าสุด. release' compile 'com.squareup.okhttp3
2. เพิ่มการตรวจสอบความคืบหน้าการดาวน์โหลด:
อินเทอร์เฟซสาธารณะ DownloadProgressListener {Void Update (Long BytesRead, Long ContentLength, Boolean เสร็จแล้ว);} Public Class DownloadProgressResponseBody ขยาย ResponseBody {Private ResponseBody ResponseBody; Private DownloadProgressListener ProgressListener; BufferedSource ส่วนตัว BufferedSource; Public DownloadProgressResponseBody (ResponseBody ResponseBody, DownloadProgressListener ProgressListener) {this.ResponseBody = ResponseBody; this.progressListener = ProgressListener; } @Override Public MediaType ContentType () {return ResponceBody.ContentType (); } @Override Public Long ContentLength () {return ResponceBody.ContentLength (); } @Override Public BufferedSource Source () {ถ้า (bufferedSource == null) {bufferedSource = okio.buffer (แหล่งที่มา (ResponceBody.source ())); } return bufferedSource; } แหล่งที่มาส่วนตัว (แหล่งที่มา) {ส่งคืนใหม่ forwardingSource (แหล่งที่มา) {long totalByTesread = 0l; @Override สาธารณะอ่านยาว (บัฟเฟอร์อ่างล้างจานยาว bytecount) พ่น IOException {long bytesRead = super.read (sink, bytecount); // read () ส่งคืนจำนวนไบต์ที่อ่านหรือ -1 ถ้าแหล่งที่มานี้หมด TotalByTesRead += bytesRead! = -1? BytesRead: 0; if (null! = progressListener) {progressListener.update (TotalByTesRead, ResponseBody.ContentLength (), bytesRead == -1); } return bytesRead; - - Public Class DownloadProgressInterceptor ใช้ Interceptor {Private DownloadProgressListener Listener; Public DownloadProgressInterceptor (DownloadProgressListener Listener) {this.listener = Listener; } @Override สกัดกั้นการตอบสนองสาธารณะ (ห่วงโซ่โซ่) พ่น IOException {ตอบสนองต้นฉบับ Response = chain.proceed (chain.request ()); ส่งคืน OriginalResponse.newBuilder () .Body (ใหม่ดาวน์โหลด ProgressResponseBody (OriginalResponse.body (), ผู้ฟัง)) .build (); -3. สร้างคลาสองค์ประกอบสำหรับการดาวน์โหลดความคืบหน้า:
การดาวน์โหลดคลาสสาธารณะใช้พัสดุ {ความคืบหน้าของเอกชน private -long currentfilesize; Totalfilesize ส่วนตัว สาธารณะ int getProgress () {ความคืบหน้ากลับ; } โมฆะสาธารณะ setProgress (ความคืบหน้า int) {this.progress = ความคืบหน้า; } สาธารณะยาว getCurrentFilesize () {return currentFilesize; } โมฆะสาธารณะ setCurrentFilesize (long currentfilesize) {this.currentFilesize = currentFilesize; } สาธารณะ GetTotalFilesize () {return totalFilesize; } โมฆะสาธารณะ settotalfilesize (totalfilesize ยาว) {this.totalfilesize = TotalFilesize; } @Override Public Int Desplecontents () {return 0; } @Override โมฆะสาธารณะ WriteToparcel (พัสดุไปรษณีย์, ธง int) {dest.writeInt (this.progress); dest.writeLong (this.currentfilesize); dest.writeLong (this.totalfilesize); } ดาวน์โหลดสาธารณะ () {} การดาวน์โหลดที่ได้รับการป้องกัน (พัสดุใน) {this.progress = in.readint (); this.currentFilesize = in.readlong (); this.totalfilesize = in.readlong (); } สาธารณะคงที่ parcelable.creator <lowwlowing> creator = ใหม่ parcelable.creator <lowwlowing> () {@Override ดาวน์โหลดสาธารณะ createFromparcel (แหล่งพัสดุ) {ส่งคืนดาวน์โหลดใหม่ (แหล่งที่มา); } @Override ดาวน์โหลดสาธารณะ [] newArray (ขนาด int) {ส่งคืนดาวน์โหลดใหม่ [ขนาด]; -4. ดาวน์โหลดคลาสเครือข่ายไฟล์:
อินเทอร์เฟซ public downloadservice {@streaming @get สังเกต <sonplemingbody> ดาวน์โหลด (@url string url);}หมายเหตุ: ที่นี่ @url เป็น URL ดาวน์โหลดที่สมบูรณ์; ไม่มีการสกัดกั้น
คลาสสาธารณะ downloadapi {private static final String tag = "downloadapi"; int คงสุดท้ายคงที่ int default_timeout = 15; ติดตั้งเพิ่มเติมสาธารณะ Public DownloadApi (url สตริง, downloadprogresslistener Listener) {downloadprogressInterceptor interceptor = ใหม่ downloadprogressInterceptor (ผู้ฟัง); ไคลเอนต์ okhttpClient = ใหม่ okhttpClient.builder () .addinterceptor (interceptor) .RetryOnConnectionFailure (จริง) .connectTimeOut (default_timeout, timeUnit.seconds) .build (); retrofit = ใหม่ retrofit.builder () .baseurl (url) .client (ไคลเอนต์) .addcalladapterfactory (rxjavacalladapterfactory.create ()) .build (); } โมฆะสาธารณะดาวน์โหลด APK (@NONNULL String URL, ไฟล์ไฟล์สุดท้าย, สมาชิกผู้สมัครสมาชิก) {log.d (TAG, "DownloadAPK:" + URL); retrofit.create (downloadservice.class) .download (url) .subscribeon (schedulers.io ()) .unsubscribeonon (schedulers.io ()) .map (ใหม่ func1 <responsebody, inputstream> () .Observeon (schedulers.Computation ()) .doonnext (ใหม่ action1 <putstream> () {@Override การเรียกโมฆะสาธารณะ (inputstream inputstream) {ลอง {fileutils.writefile (inputstream, ไฟล์);} catch (ioexception e) }) .observeon (Androidschedulers.mainThread ()) .subscribe (สมาชิก); - จากนั้นก็เรียกว่า:
เครือข่ายทำในบริการ
Public Class DownloadService ขยาย IntentService {String String ส่วนตัวแบบคงที่ส่วนตัว = "DownloadService"; Private NotificationCompat.Builder NotificationBuilder; NotificationManager Private NotificationManager; สตริงส่วนตัว apkurl = "http://download.fir.im/v2/app/install/595c5959959d6901ca0004ac?download_token=1a9dfa8f248b6e45e46bc5ed96a0a9e Public DownloadService () {super ("downloadservice"); } @Override Void Void OnhandleIntent (เจตนาเจตนา) {NotificationManager = (NotificationManager) GetSystemService (Context.Notification_Service); NotificationBuilder = netificationCompat.builder (นี่) .setsmallicon (r.mipmap.ic_download) .setContentTitle ("ดาวน์โหลด") .setContentText ("ดาวน์โหลดไฟล์") .SetAutocancel (จริง); NotificationManager.Notify (0, NotificationBuilder.build ()); ดาวน์โหลด (); } private void download () {downloadProgressListener Listener = ใหม่ downloadProgressListener () {@Override การอัปเดตโมฆะสาธารณะ (Long BytesRead, Long ContentLength, บูลีนเสร็จแล้ว) {ดาวน์โหลดดาวน์โหลด = ดาวน์โหลดใหม่ (); download.settotalFilesize (ContentLength); download.setCurrentFilesize (bytesRead); int progress = (int) ((bytesread * 100) / contentLength); download.setProgress (ความคืบหน้า); SendNotification (ดาวน์โหลด); - ไฟล์ outputFile = ไฟล์ใหม่ (environment.getExternalStoragePublicDirectory (environment.directory_downloads), "file.apk"); String baseUrl = stringUtils.getHostName (apkurl); ใหม่ downloadapi (baseUrl, ผู้ฟัง) .downloadapk (apkurl, outputfile, สมาชิกใหม่ () {@Override โมฆะสาธารณะ oncompleted () {downloadcompleted ();} @Override โมฆะสาธารณะ e.getMessage ()); } โมฆะส่วนตัว DownloadcompleTed () {ดาวน์โหลดดาวน์โหลด = ใหม่ดาวน์โหลด (); download.setProgress (100); Sendintent (ดาวน์โหลด); NotificationManager.cancel (0); NotificationBuilder.SetProgress (0, 0, FALSE); NotificationBuilder.setContentText ("ไฟล์ดาวน์โหลด"); NotificationManager.Notify (0, NotificationBuilder.build ()); } โมฆะส่วนตัว sendNotification (ดาวน์โหลดดาวน์โหลด) {sendintent (ดาวน์โหลด); NotificationBuilder.SetProgress (100, download.getProgress (), false); NotificationBuilder.SetContentText (StringUtils.getDatasize (download.getCurrentFilesize ()) + "/" + stringUtils.getDatasize (download.getTotalFilesize ()); NotificationManager.Notify (0, NotificationBuilder.build ()); } โมฆะส่วนตัว sendintent (ดาวน์โหลดดาวน์โหลด) {เจตนาเจตนา = ความตั้งใจใหม่ (mainactivity.message_progress); Intent.putextra ("ดาวน์โหลด", ดาวน์โหลด); localBroadcastManager.getInstance (downloadservice.his) .sendbroadcast (เจตนา); } @Override โมฆะสาธารณะ ontaskremoved (เจตนา rotootintent) {notificationManager.cancel (0); -รหัสหลัก:
คลาสสาธารณะ MainActivity ขยาย AppCompatactivity {String String สุดท้ายสาธารณะ Message_Progress = "Message_Progress"; AppCompatButton ส่วนตัว btn_download; ความคืบหน้าในแถบส่วนตัว Private TextView progress_text; Broadcastreceiver ส่วนตัว BroadCastreceiver = ใหม่ BroadCastreceiver () {@Override โมฆะสาธารณะ onReceive (บริบทบริบท, เจตนาเจตนา) {ถ้า (intent.get.getAction (). เท่ากัน progress.setProgress (download.getProgress ()); if (download.getProgress () == 100) {progress_text.settext ("ไฟล์ดาวน์โหลดเสร็จสมบูรณ์"); } else {progress_text.settext (stringutils.getDatasize (download.getCurrentFilesize ()) + "/" + stringUtils.getDatasize (download.getTotalFilesize ()); - @Override ป้องกันโมฆะ onCreate (Bundle SavedInstancestate) {super.oncreate (savedinstancestate); SetContentView (R.Layout.Activity_Main); btn_download = (appcompatbutton) findViewById (r.id.btn_download); ความคืบหน้า = (progressbar) findViewById (r.id.progress); progress_text = (textView) findViewById (r.id.progress_text); registerReceiver (); btn_download.setonclicklistener (ใหม่ view.onclicklistener () {@Override โมฆะสาธารณะ onclick (ดูมุมมอง) {เจตนาเจตนา = เจตนาใหม่ (mainactivity.his, downloadservice.class); startervice (เจตนา);}}); } โมฆะส่วนตัว registerReceiver () {localBroadcastManager bManager = localBroadcastManager.getInstance (นี่); IntentFilter IntentFilter = new IntentFilter (); IntentFilter.Addaction (message_progress); bmanager.registerreceiver (Broadcastreceiver, IntentFilter); -ซอร์สโค้ดของบทความนี้: การติดตั้ง RXJAVA การใช้งานไฟล์ดาวน์โหลดไฟล์
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น