美文网首页
Android下载安装apk,适配7.0以上

Android下载安装apk,适配7.0以上

作者: 所謂向日葵族 | 来源:发表于2019-03-01 15:48 被阅读0次

    本文使用DownloadManager下载,包含下载安装全过程。

    1.下载

    通过DownloadManager下载到安装包目录下/files/Download:

    public void downloadApk(String url, String appname) {

        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI); //设置在什么网络情况下进行下载 默认所有的 //

        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE); //设置通知栏

        request.setTitle(appname);

        request.setDescription(appname + "正在下载");

        request.setAllowedOverRoaming(false);

        request.setDestinationInExternalFilesDir(App.me(), Environment.DIRECTORY_DOWNLOADS, appname + ".apk");

       DownloadManager downManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);

        Long id = downManager.enqueue(request);//下载任务的id 建议存储用来查询任务

    }

    2.下载完成接收

    定义广播接收器:

    public class DownLoadCompleteReceiver 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);

                    queryDownTask(DownloadManager.STATUS_SUCCESSFUL, id);//查询任务

                } else if (intent.getAction().equals(DownloadManager.ACTION_NOTIFICATION_CLICKED)) {//通知栏点击

                }

            }

        }

    广播接收器注册:

    我采用的动态注册,记得unRegisterReceiver(receiver)取消注册哦~

    IntentFilter filter =new IntentFilter();

    filter.addAction(DownloadManager.ACTION_NOTIFICATION_CLICKED);

    filter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);

    context.registerReceiver(downLoadCompleteReceiver, filter);

    查询下载任务:

    private void queryDownTask(int status, long id) {

            DownloadManager.Query query = new DownloadManager.Query();

            query.setFilterByStatus(status);

            query.setFilterById(id);

            Cursor cursor = downManager.query(query);

            while (cursor.moveToNext()) {

                String downId = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_ID));

                String title = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_TITLE));

                String address = "";

                //注意:这样7.0以上取出来的是file:///开头

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

                    address = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));

                } else {

                    address = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));

                }

                String statuss = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));

                String size = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));

                String sizeTotal = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));

                Map<String, String> map = new HashMap<String, String>();

                map.put("downid", downId);

                map.put("title", title);

                map.put("address", address);

                map.put("status", statuss);

              //开始根据address进行安装----

            }

            cursor.close();

        }

    3.安装

    在7.0以上的安装需要获取安装未知应用权限,还需要通过FileProvider拿到content:/开始的地址。

    (1)打开安装未知来源权限

    (2)安装

    相关文章

      网友评论

          本文标题:Android下载安装apk,适配7.0以上

          本文链接:https://www.haomeiwen.com/subject/xqxduqtx.html