为什么我下载后无法安装.apk

时间:2015-12-13 15:01:15

标签: android parsing android-intent install apk

我制作了一个Android应用程序,并制作了有关更新的功能。

我下载.apk文件并使用意图来安装它。但是它总是有一个错误,例如"解析包时遇到问题"

我的代码是

我在下载完成时使用接收器来监听动作,代码是

private BroadcastReceiver mBroadcaseReceiver;
protected void onCreate(@Nullable Bundle savedInstanceState) {
mCheckUpdateBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("AboutUsActivity","check update");
            downloadApk();
        }
    });
mBroadcaseReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)){
                Log.d("aboutusactivity","下载完成");
                //下载完毕后安装
                installApk();
            }
        }
    };
    registerReceiver(mBroadcaseReceiver,new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}


private void downloadApk() {
    Log.d("AboutusActivity","update");
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse("XXXXXX"));
    request.setDescription("updating");
    request.setTitle("title");
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    }
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "yuedong.apk");

    // 获得下载服务和队列文件
    DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    manager.enqueue(request);
}

private void installApk() {
    Intent mIntent = new Intent(Intent.ACTION_VIEW);
    mIntent.setDataAndType(Uri.fromFile(new File(Environment.DIRECTORY_DOWNLOADS,"yuedong.apk")),
            "application/vnd.android.package-archive");
    this.startActivity(mIntent);
}

但总是喜欢error 那么我的代码有什么问题呢?

3 个答案:

答案 0 :(得分:0)

您下载的应用文件.apk可能已损坏。如果您尝试安装损坏的应用程序,您将收到解析错误"解析包时出现问题"。因此,请再次尝试完全下载应用程序并安装它。

答案 1 :(得分:0)

可能是因为该文件具有私有模式保护(访问权限)。尝试this链接。

答案 2 :(得分:0)

我现在知道原因 因为我下载apk的路径与我选择安装apk的路径不匹配。我很蠢。 我改变它就像

private void downloadApk(String url) {
    Log.d(TAG,"download");
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
    request.setDescription("updating");
    request.setTitle("My app");

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
    }
    request.setDestinationInExternalPublicDir("/xxx/","update.apk");
    // 获得下载服务和队列文件
    DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    manager.enqueue(request);
}

private void installApk() {
    File mFile;
    mFile = new File(Environment.getExternalStorageDirectory()+"/xxx/update.apk");
    if (mFile.exists()){
        Intent mIntent = new Intent(Intent.ACTION_VIEW);
        mIntent.setDataAndType(Uri.parse("file://"+mFile.getAbsolutePath()),
                "application/vnd.android.package-archive");
        startActivity(mIntent);
    }else {
        Log.d(TAG,"the file is not exist");
    }

}