download Apk By DownloadManager - Android App

Android examples for App:APK Download

Description

download Apk By DownloadManager

Demo Code

/*// w ww.  ja  v a2 s . c o m
 * Copyright 1999-2101 Alibaba Group.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;

import android.app.DownloadManager;
import android.content.Context;

import android.net.Uri;
import android.os.Build;
import android.os.Environment;

public class Main {
    /**
     * Note: Make sure isDownloadManagerAvailable return is true before use this method.
     * @param apkName Apk File Name
     * @param fullApkUrl url of full
     * @param context Context
     */
    public static void downloadApkByDownloadManager(String apkName,
            String fullApkUrl, Context context) {
        DownloadManager.Request request = new DownloadManager.Request(
                Uri.parse(fullApkUrl));
        request.setDescription(fullApkUrl);
        request.setTitle(apkName);

        // in order for this if to run, you must use the android 3.2 to compile your app
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        }
        request.setDestinationInExternalPublicDir(
                Environment.DIRECTORY_DOWNLOADS, apkName);
        request.setVisibleInDownloadsUi(false);
        request.setMimeType("application/vnd.android.package-archive");

        // get download service and enqueue file
        DownloadManager manager = (DownloadManager) context
                .getSystemService(Context.DOWNLOAD_SERVICE);
        manager.enqueue(request);
    }
}

Related Tutorials