android version update and download install android 7.0

The version update before android 7.0 is actually quite simple. You only need to use the system downloader to complete the download and installation. However, after android 7.0 upgrades the security mechanism, the download and installation are limited. Here I have installed several tools for developers to use:

add permission

<uses-permissionandroid:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

Next are the most important tool classes:

public class CXVersionCheckUtils {

    private static File saveFile;
    private Activity activity;
    private static long downloadId = 0;

    public CXVersionCheckUtils(Activity activity) {
        this.activity = activity;
        initFile();
    }

    private void initFile() {
        if (saveFile==null)
            saveFile=new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "filiday.apk");
    }

    public void start(String url,String t, String d){
        if (downloadId!=0) {
            clearCurrentTask(downloadId);
        }
        downloadId=download(url,t,d);
    }

    public long download(String url, String title, String desc) {
        Uri uri = Uri.parse(url);
        DownloadManager.Request req = new DownloadManager.Request(uri);
        //Set update under WIFI
        req.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
        //Display notification bar during and after downloading
        req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        //Use the default download path of the system, which is in APP / android/data/packages, so it is compatible with 7.0
//        req.setDestinationInExternalFilesDir(activity, Environment.DIRECTORY_DOWNLOADS, title);
        if (saveFile.exists()) {
            saveFile.delete();
        }
        req.setDestinationUri(Uri.fromFile(saveFile));
        //Notice bar title
        req.setTitle(title);
        //Notice column description
        req.setDescription(desc);
        //Set type to. apk
        req.setMimeType("application/vnd.android.package-archive");
        //Get download task ID
        DownloadManager dm = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE);
        return dm.enqueue(req);
    }

    public void clearCurrentTask(long downloadId) {
        DownloadManager dm = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE);
        try {
            dm.remove(downloadId);
        } catch (IllegalArgumentException ex) {
            ex.printStackTrace();
        }

    }

    public static void installApk(Context context) {
        downloadId=0;

        Intent intent = new Intent(Intent.ACTION_VIEW);
        try {
            String[] command = {"chmod", "777", saveFile.getAbsolutePath()};
            ProcessBuilder builder = new ProcessBuilder(command);
            builder.start();
        } catch (Exception ignored) {
            ignored.printStackTrace();
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(context, "com.chengxing.comchenxingnetapp.fileprovider", saveFile);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(saveFile), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }

        context.startActivity(intent);
    }


}

Create broadcast receiver: CXDownloadReceiver

public class CXDownloadReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
            long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
            CXVersionCheckUtils.installApk(context);//Start installation
        } else if (intent.getAction().equals(DownloadManager.ACTION_NOTIFICATION_CLICKED)) {
            // DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
            //Get all download task Ids groups
            //long[] ids = intent.getLongArrayExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS);
            ////Click the notification bar to cancel all downloads
            //manager.remove(ids);
            //Toast.makeText(context, "download task cancelled", toast. Length_short. Show();
            //Processing if the download has not been completed, the user clicks Notification and jumps to the download center
            Intent viewDownloadIntent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
            viewDownloadIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(viewDownloadIntent);
        }

    }
}

Configure broadcast:

        <!--Download and install the receiver-->
        <receiver android:name=".CXDownloadReceiver">
            <intent-filter>
                <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
                <action android:name="android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED" />
            </intent-filter>
        </receiver>

        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.chengxing.comchenxingnetapp.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

@XML / file path is the directory where apk can be installed:

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

The apk that allows the sd card root is used here to allow installation. android.support.v4.content.FileProvider is an Android 7.0 provider that needs to be configured.

usage method:

versionCheckUtils.start(updateBean.updateUrl,"filiday.apk", "filiday new version");

The first parameter is the download address, the second parameter is the download notification title, and the third parameter is the notification description.

Tags: Android FileProvider xml encoding

Posted on Fri, 31 Jan 2020 04:54:31 -0800 by tbone05420