Android 8.0 version update download

  Regarding the download and update of 8.0 apk one by one, because the software is used internally, there are still very few 8.0 before, and I have never known that there will be problems. But then the leader suddenly brought it up, so I updated it.

  In fact, compared with 7.0, 8.0 is actually an addition of permissions.
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

  Download the full code below


    Create a file named provider_paths in the xml under res

 

<?xml version="1.0" encoding="utf-8"?>
<paths
    <external-path

        path="." name="files_path" />
</paths>


    Create provider under application

    

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>

    java code

    Take the current version and the version returned by the server to compare, if it is lower than the server, start downloading

    The following is the download action done in the fragment.

    

//Dynamic application permission
private void quanxian(){
        RxPermissions.getInstance(getActivity())
                // request for access
                .request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Action1<Boolean>() {
                    @Override
                    public void call(Boolean granted) {
                        if(granted){
                            // request succeeded
                            showNoticeDialog();

                        }else{
                            // The request fails to recycle the current service


                        }
                    }
                });

    }

//Update the current app version
    private void showNoticeDialog() {
        // renew
        AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());

        dialog.setTitle("Detected new version, update immediately")
                .setMessage("A new version is detected, update it immediately").setMessage("Update content:\n")//The update content is written according to what the server returns when the version is fetched
                .setPositiveButton("更新", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        Toast.makeText(getActivity(), "Download in notification bar", Toast.LENGTH_SHORT).show();

                        // show the download dialog
                        showDownloadDialog();
                    }
                });
       
            dialog.setNegativeButton("Next time", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
        



        dialog.setCancelable (false);
        dialog.show();
    }


 /**
     * Show software download dialog
     */
    private void showDownloadDialog() {

        String downPath = http://xiaz.apk;//The download path is based on the apk storage path returned by the server
        //Use the system download class
        mDownloadManager = (DownloadManager) getActivity().getSystemService(DOWNLOAD_SERVICE);
        Are you = Are.parse (downPath);
        DownloadManager.Request request = new DownloadManager.Request (uri);
        request.setAllowedOverRoaming(false);

        //create directory download
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "下载.apk");
        // Save the id and use it in the receiver
        downloadId = mDownloadManager.enqueue(request);
        //Set the allowed network type, here is both mobile network and wifi
        request.setAllowedNetworkTypes (DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
        // model adaptation
        request.setMimeType("application/vnd.android.package-archive");
        //display notification bar
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setTitle("下载");
        request.setDescription("Downloading...");
        request.setVisibleInDownloadsUi(true);
        getActivity().registerReceiver(mReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    }



private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            checkStatus();
        }
    };


/**
     * Check download status
     */
    private void checkStatus() {
        DownloadManager.Query query = new DownloadManager.Query ();
        query.setFilterById(downloadId);
        Cursor cursor = mDownloadManager.query(query);
        if (cursor.moveToFirst()) {
            int status = cursor.getInt (cursor.getColumnIndex (DownloadManager.COLUMN_STATUS));
            switch (status) {
                // download paused
                case DownloadManager.STATUS_PAUSED:
                    break;
                // download delay
                case DownloadManager.STATUS_PENDING:
                    break;
                //downloading
                case DownloadManager.STATUS_RUNNING:
                    break;
                //Download completed
                case DownloadManager.STATUS_SUCCESSFUL:
                    installAPK();
                    break;
                //download failed
                case DownloadManager.STATUS_FAILED:
                    Toast.makeText(getActivity(), "Download failed", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
        cursor.close();
    }

    /**
     * 7.0 Compatible
     */
    private void installAPK() {
        File apkFile =
                new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "下载.apk");
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Uri apkUri = FileProvider.getUriForFile(getActivity(), getActivity().getPackageName() + ".provider", apkFile);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
        }
        getActivity().startActivity(intent);
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324406507&siteId=291194637