Powerful_cat
2017-02-28 07:00
采纳率: 3.8%
浏览 5.0k
已采纳

Android app检测版本并下载apk后实现覆盖安装

请问这个功能怎么做呢 大家是用的官方的DownloadManager 吗 为啥我这能下载就是不能提示安装 哪位大神有好的方法 确实挺急的 在线等!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • crystal2fiona 2017-02-28 08:44
    已采纳

    /**
    * 安装apk
    */
    public static void installApk(Context context,String fileName)
    {
    File file = new File(fileName);
    chmod("777", file.getAbsolutePath());
    Intent intent = new Intent();
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setAction(Intent.ACTION_VIEW);
    String type = "application/vnd.android.package-archive";
    intent.setDataAndType(Uri.fromFile(file), type);
    context.startActivity(intent);
    }

     用DownloadManager下载并 注册广播及时监听下载完成状态 receiver = new DownloadCompleteReceiver();
                mContext.registerReceiver(receiver, new IntentFilter(
                        DownloadManager.ACTION_DOWNLOAD_COMPLETE));,在广播接收处里调用installApk方法,还可以定制相关提示信息
    
                                                class DownloadCompleteReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(
                    DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
                mDownloadCustomViewDialog.dismiss();
                AppCookie.getInstance().putDownloadVersion(version);
                DialogUtil.showToastDialog(mContext, "下载成功...");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                AndroidSystemInfo.installApk(mContext, apkPath);
                if (receiver != null){
                    mContext.unregisterReceiver(receiver);
                    BaseActivity activity = (BaseActivity) mContext;
                    activity.finish();
                }
            }
        }
    
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • yaochaoyang 2017-02-28 07:42

    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    评论
    解决 无用
    打赏 举报
  • crystal2fiona 2017-03-01 01:39
      DownloadManager.Request request = new DownloadManager.Request(
                            Uri.parse(url));
                    request.setDestinationInExternalPublicDir("ChinaCloudproviders",
                            "ChinaCloudproviders.apk");
                    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE
                            | DownloadManager.Request.NETWORK_WIFI);
                    request.setShowRunningNotification(false);
                    request.setVisibleInDownloadsUi(false);
                    request.setMimeType("application/vnd.android.package-archive");
    

    三个路径:
    public final static String PATH = Environment.getExternalStorageDirectory().getAbsolutePath()+"";//sd卡根目录路径
    private String dirPath = AndroidSystemInfo.PATH + "/ChinaCloudproviders";
    private String apkPath = AndroidSystemInfo.PATH + "/ChinaCloudproviders/ChinaCloudproviders.apk";

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题