1. 程式人生 > >Android OkGo下載更新APK實現程式碼

Android OkGo下載更新APK實現程式碼

直接上程式碼:

  final String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/download";
        final String fileName = "abc.apk";
        File file = new File(filePath + "/" + fileName);
        if (file.exists())
            file.delete();
        OkGo.<File>get(url).execute(new FileCallback(filePath, fileName) {

            @Override
            public void onStart(Request<File, ? extends Request> request) {
                super.onStart(request);
                if (fullScreen) {
                    coverMainImageView.setVisibility(View.VISIBLE);
                    numberProgressBar.setVisibility(View.VISIBLE);
                } else {
                    downloadProgress.show();
                }

            }

            @Override
            public void onSuccess(Response<File> response) {

            }

            @Override
            public void onError(Response<File> response) {
                super.onError(response);
                if (fullScreen) {
                    coverMainImageView.setVisibility(View.INVISIBLE);
                    numberProgressBar.setVisibility(View.INVISIBLE);
                } else {
                    downloadProgress.dismiss();
                }
            }

            @Override
            public void downloadProgress(Progress progress) {
                super.downloadProgress(progress);
                int percentProgress = (int) (progress.fraction * 100);
                if (fullScreen) {
                    numberProgressBar.setProgress(percentProgress);
                } else {
                    downloadProgress.setProgress(percentProgress);
                }
            }

            @Override
            public void onFinish() {
                super.onFinish();
                if (fullScreen) {
                    coverMainImageView.setVisibility(View.INVISIBLE);
                    numberProgressBar.setVisibility(View.INVISIBLE);
                } else {
                    downloadProgress.dismiss();
                }
                Intent intent = new Intent(Intent.ACTION_VIEW);
                File file = new File(filePath + "/" + fileName);
                Uri uri;
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    uri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".MyFileProvider", file);
                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                } else {
                    uri = Uri.fromFile(file);
                }
                intent.setDataAndType(uri, "application/vnd.android.package-archive");
                startActivity(intent);

            }
        });
    }
MyFileProvider.java  //空實現
public class MyFileProvider extends FileProvider {
}

清單檔案中

 <provider
            android:name=".provider.MyFileProvider"
            android:authorities="com.theninepro.cn.thenineproject.MyFileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

:file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="mydownload"
        path="/download"/>
</paths>