2017-05-12 96 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