2015-08-13 201 views
-4

我在某些應用程序中看到了這個,應用程序大小約爲6 MB,但它下載了一個大約100kb的文件並更新了應用程序。android-如何更新應用程序內的應用程序

這非常有趣,我搜索了很多,但我找不到任何方式來做到這一點。

我該怎麼做?

感謝

+0

什麼讓你感興趣,只需創建具有結構簡單的應用程序和發佈,當它打開了第一次下載所有從你的服務器,即是所謂的「增量更新」的數據,簡單 – Sree

+0

。 –

回答

0

我使用下面的類,但它確實需要下載新的APK,所以它可能不是正是你需要的。這樣做是因爲我們不使用Play商店。 如果有更新可用,請啓動Runnable類。 它開始下載,下載完成後會詢問您是否要更新,然後開始更新。 所有你需要做的就是弄清楚如何託管APK文件。我使用Windows服務器和IIS7,使用mime設置,因此它被android認定爲可安裝的APK。

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.IOException; 

import android.app.AlertDialog; 
import android.app.DownloadManager; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.net.Uri; 
import android.os.Environment; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ProgressBar; 
import android.widget.Toast; 

public class GetUpdate implements Runnable{ 
    Context cxt; 
    String line; 
    String filepath = ""; 
    int continueornot=0; 
    ProgressBar progBar; 
    Button buttOk; 
    DownloadManager mgr=null; 
    long lastDownload=-1L; 


    public GetUpdate(Context contextIn, String lineIn, ProgressBar progressBar,Button okButtIn){ 
     cxt = contextIn; 
     line = lineIn; 
     this.progBar = progressBar; 
     this.buttOk = okButtIn; 


    } 

    @Override 
    public void run() { 
     filepath = cxt.getExternalFilesDir("/MyFileStorage/").getAbsolutePath(); 
     AlertDialog.Builder alert = new AlertDialog.Builder(cxt); 

     alert.setTitle("Update Availible"); 
     alert.setMessage("Start the download?"); 

     // Set an EditText view to get user input 
     //final EditText serverURL = new EditText(cxt); 
     //alert.setView(serverURL); 

     alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 
       //String tempFilepath = cxt.getExternalFilesDir("/MyFileStorage/").getAbsolutePath(); 
       File myExternalFile = new File(filepath); 

       File[] sdDirList = myExternalFile.listFiles(); 
       if(sdDirList != null){ 
        for(int x=0;x<sdDirList.length;x++){ 
         String fileNameString = sdDirList[x].toString(); 
         System.out.println("File: " + sdDirList[x].toString()); 

         if(fileNameString.trim().equalsIgnoreCase("podcodes.txt") 
           ||fileNameString.trim().equalsIgnoreCase("vehiclesTrailers.txt")         
           ||fileNameString.trim().equalsIgnoreCase("checks.txt") 
           ||sdDirList[x].toString().endsWith(".apk")){ 

          sdDirList[x].delete(); 
         } 
        } 
       } 

       BroadcastReceiver onComplete=new BroadcastReceiver() { 
        public void onReceive(Context ctxt, Intent intent) { 

         AlertDialog.Builder alert = new AlertDialog.Builder(cxt); 

         alert.setTitle("Update Availible"); 
         alert.setMessage("Start the update?"); 

         alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int whichButton) { 
           Toast.makeText(cxt.getApplicationContext(), "Updating!", Toast.LENGTH_LONG).show(); 
           Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE); 
           String lastDownloaded = mgr.getUriForDownloadedFile(lastDownload).toString(); 
           //String lastDownloadFileName = lastDownloaded.substring(lastDownloaded.lastIndexOf("/")+1); 
           intent.setDataAndType(Uri.parse(lastDownloaded), "application/vnd.android.package-archive"); 
           intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
           cxt.startActivity(intent); 
           Globals.setExit(true); 
          } 
         }); 

         alert.setNegativeButton("No", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int whichButton) { 
           progBar.setVisibility(View.GONE); 
           buttOk.setText("OK"); 
           buttOk.setEnabled(true); 
           buttOk.setVisibility(View.VISIBLE); 

          } 
         }); 

         alert.show(); 
        } 
       }; 

       mgr=(DownloadManager)cxt.getSystemService(Context.DOWNLOAD_SERVICE); 
       cxt.registerReceiver(onComplete, 
         new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); 

       BroadcastReceiver onNotificationClick=new BroadcastReceiver() { 
        public void onReceive(Context ctxt, Intent intent) { 
         Toast.makeText(ctxt, "Downloading InCab Update!", Toast.LENGTH_LONG).show(); 
        } 
       }; 
       cxt.registerReceiver(onNotificationClick, 
         new IntentFilter(DownloadManager.ACTION_NOTIFICATION_CLICKED)); 


       Uri uri=Uri.parse(Globals.getServerURL()+"/LatestAndroid/"+line.trim()); 
       //Environment 
       // .getExternalStoragePublicDirectory("MyFileStorage/"+line.trim()) 
       // .mkdirs(); 

       lastDownload= 
         mgr.enqueue(new DownloadManager.Request(uri) 
         .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | 
           DownloadManager.Request.NETWORK_MOBILE) 
           .setAllowedOverRoaming(true) 
           .setTitle(line.trim()) 
           .setDescription("Incab Update.") 
           .setDestinationInExternalFilesDir(cxt,"MyFileStorage", line.trim())); 




       Toast.makeText(cxt.getApplicationContext(), "Downloading!", Toast.LENGTH_LONG).show(); 
       continueornot=1; 
       progBar.setVisibility(View.VISIBLE);   
       buttOk.setVisibility(View.VISIBLE); 
       buttOk.setText("Downloading.."); 
       buttOk.setEnabled(false); 
      } 
     }); 

     alert.setNegativeButton("No", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 
       continueornot=2;     
       progBar.setVisibility(View.GONE); 
       buttOk.setText("OK"); 
       buttOk.setEnabled(true); 
       buttOk.setVisibility(View.VISIBLE); 
       //cancel(true); 
      } 
     }); 

     alert.show(); 

     progBar.setVisibility(View.GONE); 
     buttOk.setText("OK"); 
     buttOk.setEnabled(true); 
     buttOk.setVisibility(View.VISIBLE); 

    } 


}