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

android-如何在应用程序内部更新应用程序,android,Android,我在一些应用程序中看到过这种情况,应用程序大小约为6MB,但它下载了一个约100kb的文件并更新应用程序 这很有趣,我搜索了很多,但我找不到任何方法 我怎样才能做到 谢谢我使用下面的类来做这件事,但它确实需要下载新的APK,所以它可能不是您所需要的。这样做是因为我们不使用play store。 如果有可用的更新,请启动Runnable类。 它开始下载,下载完成后,它会询问您是否要更新,然后开始更新。 您所需要做的就是弄清楚如何托管APK文件。我使用windows服务器和IIS7,并带有mime设

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

这很有趣,我搜索了很多,但我找不到任何方法

我怎样才能做到


谢谢

我使用下面的类来做这件事,但它确实需要下载新的APK,所以它可能不是您所需要的。这样做是因为我们不使用play store。 如果有可用的更新,请启动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);

    } 


}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入android.app.AlertDialog;
导入android.app.DownloadManager;
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.content.IntentFilter;
导入android.net.Uri;
导入android.os.Environment;
导入android.view.view;
导入android.widget.Button;
导入android.widget.ProgressBar;
导入android.widget.Toast;
公共类GetUpdate实现可运行{
上下文cxt;
弦线;
字符串filepath=“”;
int continueorno=0;
ProgressBar-progBar;
钮扣;
DownloadManager mgr=null;
长上次下载=-1L;
public GetUpdate(上下文上下文输入、字符串行输入、进程栏、按钮行输入){
cxt=contextIn;
line=lineIn;
this.progBar=progressBar;
this.buttOk=okButtIn;
}   
@凌驾
公开募捐{
filepath=cxt.getExternalFilesDir(“/MyFileStorage/”).getAbsolutePath();
AlertDialog.Builder alert=新建AlertDialog.Builder(cxt);
alert.setTitle(“更新可用”);
setMessage(“开始下载?”);
//设置EditText视图以获取用户输入
//最终EditText服务器URL=新的EditText(cxt);
//alert.setView(serverURL);
alert.setPositiveButton(“是”,新的DialogInterface.OnClickListener(){
public void onClick(对话框接口对话框,int whichButton){
//字符串tempFilepath=cxt.getExternalFilesDir(“/MyFileStorage/”).getAbsolutePath();
File myExternalFile=新文件(filepath);
文件[]sdDirList=myExternalFile.listFiles();
if(sdDirList!=null){

对于(int x=0;x)什么让你感兴趣,只需创建一个具有结构的简单应用程序并发布它,当它第一次打开时,从你的服务器下载所有数据,simpleThat被称为“增量更新”。