2017-05-12 95 views
0

我已经看到了一些与此相关的答案,但似乎无法找到我在找什么。假设我有一个自己托管的应用程序。现在说我已经对该应用程序进行了一些更改,并希望在应用程序中告知用户有更新可用。我可以让应用程序成功下载apk文件并开始安装它。安装完成后,应用程序关闭。当我重新启动应用程序时,我所做的任何更改都未应用。所以看起来安装失败了,但没有明显的崩溃。但是,当我安装从Downloads下载管理器下载的apk时,它安装得很好,并且我所做的更改已被应用。有任何想法吗?以下是我用来下载和安装程序的代码部分:编程式下载和安装APK

String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/"; 
String fileName = "TheApp.apk"; 
destination += fileName; 
final Uri uri = Uri.parse("file://" + destination); 

String url = "myapplocation"; 

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); 
request.setDescription("Downloading necessary update files."); 
request.setTitle("Updating The App"); 

final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); 
final long downloadId = manager.enqueue(request); 

BroadcastReceiver onComplete = new BroadcastReceiver() { 
    public void onReceive(Context ctxt, Intent intent) { 
     Intent install = new Intent(Intent.ACTION_VIEW); 
     install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     install.setDataAndType(uri, 
       manager.getMimeTypeForDownloadedFile(downloadId)); 

       startActivityForResult(install, 0); 

       unregisterReceiver(this); 
    } 
}; 
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); 
+0

你讲的不是下载管理器在哪里保存下载的文件。也不是目录。也不在哪个文件名下。使用setDestinationUri()。 – greenapps

回答

0

获取当前正在运行的应用程序的VersionName和VersionCode。 代码:

   try { 
         PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0); 
         Common.VersionName = pInfo.versionName; 
         Common.VersionCode = pInfo.versionCode; 
         Log.e("VersionCode", ">>>>>>>>>>" + Common.VersionCode + Common.VersionName); 
        } catch (PackageManager.NameNotFoundException e) { 
         e.printStackTrace(); 
        } 
**Check the Version Names** 
       if (!Common.VersionName.equals(Common.VersionNamefromWebApi)) { 
         AlertDialogUpdate(MakeTransactionActivity.this, Common.AppUpdateTitle, "YokoYepi Version" + Common.VersionNamefromWebApi + " available."); 
        } 
**Alert Dialog Box**  
    public void AlertDialogUpdate(Activity activity, String title, CharSequence message) { 
      AlertDialog.Builder builder = new AlertDialog.Builder(activity); 
      builder.setCancelable(false); 
      if (title != null) builder.setTitle(title); 
      builder.setMessage(message); 

      builder.setPositiveButton("UPDATE", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        new DownloadNewVersion().execute(); 
        dialog.dismiss(); 
       } 
      }); 
      builder.show(); 
     }  
**Download and Install the .apk file from URL**  
    class DownloadNewVersion extends AsyncTask<String, Integer, Boolean> { 
      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       bar = new ProgressDialog(MakeTransactionActivity.this); 
       bar.setCancelable(false); 
       bar.setMessage("Downloading..."); 
       bar.setIndeterminate(true); 
       bar.setCanceledOnTouchOutside(false); 
       bar.show(); 
       stoptimertask(); 
      } 

      protected void onProgressUpdate(Integer... progress) { 
       super.onProgressUpdate(progress); 
       bar.setIndeterminate(false); 
       bar.setMax(100); 
       bar.setProgress(progress[0]); 
       String msg = ""; 
       if (progress[0] > 99) { 
        msg = "Finishing... "; 
       } else { 
        msg = "Downloading... " + progress[0] + "%"; 
       } 
       bar.setMessage(msg); 
      } 

      @Override 
      protected void onPostExecute(Boolean result) { 
       super.onPostExecute(result); 
       startTimer(); 
       bar.dismiss(); 
       if (result) { 
        Toast.makeText(getApplicationContext(), "Update Done", 
          Toast.LENGTH_SHORT).show(); 
       } else { 
        Toast.makeText(getApplicationContext(), "Error: Try Again", 
          Toast.LENGTH_SHORT).show(); 
       } 
      } 

      @Override 
      protected Boolean doInBackground(String... arg0) { 
       Boolean flag = false; 
       try { 
        String PATH; 
        Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); 
        if (isSDPresent) { 
         PATH = Environment.getExternalStorageDirectory() + "/Download/"; 
        } else { 
         PATH = Environment.getDataDirectory() + "/Download/"; 
        } 
        File file = new File(PATH); 
        file.mkdirs(); 
        File outputFile = new File(file, "yokoyepi.apk"); 
        if (outputFile.exists()) { 
         outputFile.delete(); 
        } 
        // Download File from url 
        URL u = new URL(Common.AppUpdateURL); 
        URLConnection conn = u.openConnection(); 
        int contentLength = conn.getContentLength(); 

        DataInputStream stream = new DataInputStream(u.openStream()); 

        byte[] buffer = new byte[contentLength]; 
        stream.readFully(buffer); 
        stream.close(); 

        DataOutputStream fos = new DataOutputStream(new FileOutputStream(outputFile)); 
        fos.write(buffer); 
        fos.flush(); 
        fos.close(); 
        // Install dowloaded Apk file from Devive---------------- 
        OpenNewVersion(PATH); 
        flag = true; 
       } catch (MalformedURLException e) { 
        Log.e(TAG, "Update Error: " + e.getMessage()); 
        flag = false; 
       } catch (IOException e) { 
        Log.e(TAG, "Update Error: " + e.getMessage()); 
        flag = false; 
       } catch (Exception e) { 
        Log.e(TAG, "Update Error: " + e.getMessage()); 
        flag = false; 
       } 
       return flag; 
      } 

     }   
    void OpenNewVersion(String location) { 
      Intent intent = new Intent(Intent.ACTION_VIEW); 
      intent.setDataAndType(Uri.fromFile(new File(location + "yokoyepi.apk")), 
        "application/vnd.android.package-archive"); 
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      startActivity(intent); 
     } 
// if your not install u should call the function in onResume(). 
// again it will check whether apk updated or not. 
+0

您能否请您提供意见或某种结论来解释您做了什么以及您的代码如何解决OP所问的问题? –

+0

你现在可以检查吗? - 等字段 – vels