android - 如何在应用程序内更新应用程序

时间:2015-08-13 11:50:20

标签: android

我在某些应用程序中看到过这种情况,应用程序大小约为6 MB,但它下载了大约100kb的文件并更新了应用程序。

非常有趣,我搜索了很多,但我找不到任何办法。

我该怎么办?

谢谢

1 个答案:

答案 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);

    } 


}