2015-08-13 116 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); 

    } 


}