6. Download apk (implementation of the afinal framework and multi‑thread breakpoint download) afinal Introduction: idea. The apk file is in the bin directory of the Project. Change the file version in the configuration file, and then put the changed apk file into the project in the tomcat environment, in this way, the upgraded version file is obtained. Code:
FinalHttp finalHttp = new FinalHttp (); // create a FinalHttp object for multi-threaded breakpoint download File file = new File (Environment. getExternalStorageDirectory (), "temp.apk"); // you can specify finalHttp as the Directory and file name. download (updateInfo. getApkurl (), file. getAbsolutePath (), new AjaxCallBack <File> () {// download, rewrite 3 methods/*** if the download fails, print out Error */@ Override public void onFailure (Throwable t, int errorNo, String strMsg) {t. printStackTrace (); super. onFailure (t, errorNo, strMsg);}/*** method called during file download (Progress) * @ param count total file length * @ param current download progress */@ Override public void onLoading (long count, long current) {int progress = (int) (current * 100/count); TV _splash_progress.setText ("download progress:" + progress + "%"); super. onLoading (count, current);}/*** Method for successfully calling the File Download */@ Override public void onSuccess (File t) {Toast. makeText (getApplicationContext (), "Download successful, please replace and install", Toast. LENGTH_SHORT ). show (); super. onSuccess (t );}});
Add a TextView to the layout file to display the download progress. Set it to hide: code:
<TextView android:id="@+id/tv_splash_progress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ff0000" android:visibility="invisible" android:layout_below="@+id/progressBar1" android:layout_centerHorizontal="true" android:layout_marginTop="16dp"/>
Set it to visible before download, that is
tv_splash_progress.setVisibility(View.VISIBLE);
Note: Set write sdcard permission: WRITE_EXTERNAL_STORAGE