Android下载和安装Apk功能的实现

1.创建下载监听器DownloadListener

public interface DownloadListener {
    public void onProgress(int progress);
    public void onFailed();
    public void onCanceled();
    public void onSuccess(Uri uri);
}

2.使用AsyncTask实现异步下载操作

import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.support.v4.content.FileProvider;
import android.support.v4.media.MediaBrowserCompat;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class DownloadTask extends AsyncTask<String, Integer, Integer> {

    private static final int TYPE_FAILED = 1000;
    private static final int TYPE_CANCELED = 2000;
    private static final int TYPE_SUCCESS = 3000;
    private DownloadListener downloadListener;
    private boolean isCanceled = false;
    private int downloadProgress = 0;
    private Context context = null;
    private Uri fileUri;



    public DownloadTask(Context context, DownloadListener downloadListener) {
        this.downloadListener = downloadListener;
        this.context = context;
    }



    @Override
    protected void onPreExecute() {}



    @Override
    protected Integer doInBackground(String... params) {
        int type = 0;
        HttpURLConnection connection = null;
        BufferedInputStream bis = null;
        OutputStream os = null;
        InputStream is = null;
        try {
            URL downloadUrl = new URL(params[0]);
            connection = (HttpURLConnection) downloadUrl.openConnection();
            connection.setRequestMethod("GET");
            connection.setReadTimeout(20000);
            connection.setConnectTimeout(20000);
            connection.setRequestProperty("Accept-Encoding", "identity");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.connect();

            if (connection.getResponseCode() == 200 ) {
                String path = connection.getURL().getFile();
                String fileName = path.substring(path.lastIndexOf(File.separatorChar) + 1);  // 文件名称
                path = context.getExternalFilesDir("download").getAbsolutePath(); // 文件保存路径

                // 清理文件夹下的文件
                for (File f : new File(path).listFiles()) {
                    f.delete();
                }
                // 创建文件
                String filePath = path + "/" + fileName; // 文件的路径
                File file = new File(filePath);
                file.createNewFile();

                is = connection.getInputStream();
                bis = new BufferedInputStream(is);
                os = new FileOutputStream(file);
                byte[] bytes = new byte[1024];
                long currentLength = 0;
                int size = 0;
                while ( (size = bis.read(bytes)) != -1) {
                    if (isCanceled) {
                        type = TYPE_CANCELED;
                    } else {
                        currentLength += size;
                        os.write(bytes, 0, size);
                        // getContentLength()获取文件大小,当值大于Integer.MAX_VALUE时,返回-1
                        publishProgress((int)(100*currentLength/connection.getContentLength()));
                    }
                }
                fileUri = file2Uri(file);
                type =  TYPE_SUCCESS;
             }
        } catch (MalformedURLException e) {
            e.printStackTrace();
            type = TYPE_FAILED;
        } catch (IOException e) {
            e.printStackTrace();
            type = TYPE_FAILED;
        } finally {
            if (null != connection) {
                connection.disconnect();
            }
            try {
                if (null != is) {
                    is.close();
                }
                if (null != os) {
                    os.close();
                }
                if (null != bis) {
                    bis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return type;
    }



    @Override
    protected void onProgressUpdate(Integer... values) {
        if (values[0] > downloadProgress) {
            downloadProgress = values[0];
            downloadListener.onProgress(downloadProgress);
        }
    }



    @Override
    protected void onPostExecute(Integer integer) {
        switch (integer) {

            case TYPE_FAILED:
                downloadListener.onFailed();
                break;

            case TYPE_CANCELED:
                downloadListener.onCanceled();
                break;

            case TYPE_SUCCESS:
                downloadListener.onSuccess(fileUri);
                break;

            default:
                break;
        }
    }


    public void cancelDownload() {
        isCanceled = true;
    }


    /**
     * File对象并转化成Uri返回
     * @return
     */
    private Uri file2Uri(File file) {
        Uri uri = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            uri = FileProvider.getUriForFile(context, "cn.com.pirate.demo.fileprovier", file);
        } else {
            uri = Uri.fromFile(file);
        }
        return uri;
    }
}

3.在服务中操作DownloadTask

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.FileProvider;
import android.util.Log;
import java.io.File;


public class DownloadService extends BaseService {

    private static final int NOTIFICATION_ID = 1000;
    private static final int DOWNLOAD_SUCCESS = 101;
    private final Context context = this;
    private DownloadTask downloadTask = null;
    private DownloadBinder downloadBinder = new DownloadBinder();

    private DownloadListener downloadListener = new DownloadListener() {
        @Override
        public void onProgress(int progress) {
            startForeground(NOTIFICATION_ID, getNotification(null, progress));
        }

        @Override
        public void onFailed() {

        }

        @Override
        public void onCanceled() {

        }

        @Override
        public void onSuccess(Uri uri) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            // 赋予FLAG_GRANT_READ_URI_PERMISSION,FLAG_GRANT_WRITE_URI_PERMISSION权限,否则7.0以上版本无法安装
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            intent.setDataAndType(uri, "application/vnd.android.package-archive");
            startActivity(intent);

            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.notify(NOTIFICATION_ID, getNotification(pendingIntent, DOWNLOAD_SUCCESS));
            notificationManager.cancel(NOTIFICATION_ID);
        }
    };


    public class DownloadBinder extends Binder {

        public void download(String downloadUrl) {
            if (null != downloadUrl && null == downloadTask) {
                downloadTask = new DownloadTask(context, downloadListener);
                downloadTask.execute(downloadUrl);
            }
        }

        public void cancel () {
            if (null != downloadTask) {
                downloadTask.cancelDownload();
            }

        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return Service.START_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return downloadBinder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    private Notification getNotification(PendingIntent pendingIntent, int progress) {
        Notification notification = new NotificationCompat.Builder(this)
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round))
                .setWhen(System.currentTimeMillis())
                .setContentTitle(getResources().getString(R.string.app_name) + "更新")
                .setContentText(progress == DOWNLOAD_SUCCESS ? "应用下载已完成,请点击安装" : "当前已经下载 " + progress + " %")
                .setProgress(100, progress, false)
                .build();
        return notification;
    }


        public static void start(Context context) {
        Intent intent = new Intent(context, DownloadService.class);
        context.startService(intent);
    }

    public static void stop(Context context) {
        Intent intent = new Intent(context, DownloadService.class);
        context.stopService(intent);
    }

    public static boolean bind(Context context, ServiceConnection conn) {
        Intent intent = new Intent(context, DownloadService.clzss);
        context.bindService(intent, conn ,BIND_AUTO_CREATE);
        return  true;
    }

    public static boolean unbind(Context context, ServiceConnection conn) {
        context.unbindService(conn);
        return false;
    }}

4.调用下载功能

import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity {

    private static final String downloadUrl = "apk的下载地址"
    private boolean isbind = false;
    private DownloadService.DownloadBinder downloadBinder;

    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            downloadBinder = (DownloadService.DownloadBinder)service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {}
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        DownloadService.start(this);
        isbind = DownloadService.bind(this, conn);

    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,576评论 6 515
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,515评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 168,017评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,626评论 1 296
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,625评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,255评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,825评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,729评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,271评论 1 320
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,363评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,498评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,183评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,867评论 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,338评论 0 24
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,458评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,906评论 3 376
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,507评论 2 359

推荐阅读更多精彩内容