Android替换应用程序无法安装

Com*_*der 4 android upgrade

我的应用程序实现了自动升级功能,因为该应用程序不会向市场发布.apk文件托管在网络服务器上.我已经实现了以下链接的第一部分

使用AsyncTask并在对话框中显示下载进度

现在,当我运行应用程序时,会发生以下情况.

  1. 如果有新版本可用,则应用程序会提示用户是否要升级. 升级提示

  2. 如果用户选择"是",则应用程序从服务器下载文件. 下载

  3. 完成后,应用程序会提示用户是否要更换应用程序 替换提示
  4. 如果用户点击确定,则应用程序开始安装但在以下屏幕中失败 在此输入图像描述

没有提供关于为何发生这种情况的日志文件信息.请有人帮我理解可能导致此问题的原因.这是生成fianl 2屏幕而不是我的代码的默认android代码.

在异步任务上运行以下代码后会发生这种情况:

    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        // dismiss the progress dialog
        mProgressDialog.dismiss();
        // get the file
        String path = getFilesDir().getAbsolutePath()+ File.separator + getString(R.string.apk_file);
        File file = new File(path);
        // if it exists
        if(file.exists()) {
            // create and start the intent with the new file
            Intent intent = new Intent(Intent.ACTION_VIEW); 
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); 
            startActivity(intent);
        } else {
            // display error message 
            Toast.makeText(context, "File was not downloaded", Toast.LENGTH_LONG).show();
        }           
    }
Run Code Online (Sandbox Code Playgroud)

如果我卸载应用程序并使用浏览器转到文件位置,应用程序会自动下载,然后我可以从下载文件夹中成功安装它.

这可能是应用程序当前正在运行时应用程序正在尝试安装自己的问题吗?在升级过程中文件被下载到内部存储器而不是SD卡位置,因为我不能保证用户设备将存在SD卡.

Jin*_*n35 5

  1. 确保旧的和新的apks使用相同的密钥签名并具有相同的包名称.否则你会看到同样的错误.

  2. 尝试在之后调用finish您的活动(或只是使用System.exit)startActivity.