Android 从服务器下载最新APP并更新当前

该博客详细介绍了在Android平台上如何实现APP的自动更新。首先,通过获取当前APP的版本号并与服务器上的最新版本进行比较,判断是否需要更新。如果需要更新并且不是强制更新,会显示下载对话框并开始下载APK。下载完成后,通过Intent启动安装过程。整个流程涵盖了版本检查、文件下载和安装等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android 从服务器下载最新APP并更新当前

首先获取当前最新的APP版本号

public long getCurrentVersionCode(){
    long CurrentVersionCode = 0;
    try {
        PackageInfo packageInfo = context.getApplicationContext().getPackageManager().getPackageInfo(context.getPackageName(), 0);
        CurrentVersionCode = packageInfo.versionCode;
    } catch (PackageManager.NameNotFoundException e) {

    }
    return CurrentVersionCode;
};

当前版本号跟获取的最新版本号对比 同时判断是否是强制更新,强制更新取消则关闭应用

传入最新apk的对应下载地址

public void downloadApl(final String apkurl){
    Toast.makeText(context, "开始下载"+apkurl, Toast.LENGTH_SHORT).show();
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setView(inflate);
    builder.create();
    builder.show();
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                URL url = new URL(apkurl);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                final int contentLength = connection.getContentLength();//文件大小
                InputStream inputStream = connection.getInputStream();
                File file = new File(saveFilePath);
                if(!file.exists()){
                    file.mkdir();
                }
                String apkFile = saveFileName;
                File apkfile = new File(apkFile);
                FileOutputStream fileOutputStream = new FileOutputStream(apkFile);
                int count = 0;
                int lenth = 0;
                byte[] bytes = new byte[512];
                while( (lenth = inputStream.read(bytes))!=-1){
                    count+=lenth;
                    fileOutputStream.write(bytes,0,lenth);
                    Message message = new Message();
                    message.arg1 = count;
                    message.arg2 = contentLength;
                    message.what = 2;
                    handler.sendMessage(message);
                    fileOutputStream.flush();
                }
                handler.sendEmptyMessage(1);
                fileOutputStream.close();
                inputStream.close();

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

将下载好的apk安装到本机

//安装APK
public void installApl(){
    File file = new File(saveFilePath,"apkName.apk");
    if(!file.exists()){
        return;
    }

    Intent intent=new Intent(Intent.ACTION_VIEW);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//判断版本大于等于7.0
        String string = "com.example.project_class6_sc"+".fileprovider";
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_VIEW);
        Uri uri = FileProvider.getUriForFile(context.getApplicationContext(), string, file);
        intent.setDataAndType(uri,"application/vnd.android.package-archive");
    }else{
        intent.setDataAndType(Uri.parse("file://"+file.toString()),"application/vnd.android.package-archive");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    }
    context.startActivity(intent);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Android诚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值