[android]下载后为什么不能安装.apk

发布时间: 2016/9/26 4:13:31
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

Android 我和我的应用程序,使有关更新的功能。

下载.apk 文件并使用 intent 来安装它。但它总是有一个像"解析包时,出现了一个问题"的错误

我的代码

用于接收听行动,当下载完成,代码是

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是什么错误我的代码呢?

解决方法 1:

我现在知道的原因,因为我 path 的下载 apk 文件是不匹配 path ,我选择安装 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");
    }

}
赞助商