Android客户端apk自动检测更新自动下载自动安装的实现方法

本文介绍了如何实现Android应用的自动检测版本更新、删除历史下载文件以及自动下载安装的功能,旨在提供一个优化的客户端升级方案。

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

改进了一个可以检测版本更新自动下载自动安装的客户端升级方案。

在下载之前删除之前的历史下载文件,减少垃圾数据。

先给出核心类

public class DownloadService extends Service {
    private DownloadManager mDownloadManager;
    private long enqueue;
    private BroadcastReceiver receiver;
    private  static final String APK_URL= IPAddress.DEFAULT_IP+"/portrait/app-youni.apk";
//    private  static final String APK_NAME="youni.apk";
    private  static final String APK_NAME="youni_"+ System.currentTimeMillis()+"_.apk";

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        DebugLog.v("onBind");
        return null;
    }
    @Override
    public void onCreate() {
        DebugLog.v("onCreate");
        super.onCreate();
        /*删除以前下载的安装包*/
        RecursionDeleteFile(new File(Environment.getExternalStorageDirectory() + "/download/youni/"));
    }

    @Override
    public void onStart(Intent intent, int startId) {
        DebugLog.v("onStart");
        super.onStart(intent, startId);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        DebugLog.v("onStartCommand");
        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                intent = new Intent(Intent.ACTION_VIEW);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/youni/"+APK_NAME)),
                        "application/vnd.android.package-archive");
                startActivity(intent);
                stopSelf();
            }
        };
        registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
        startDownload();
        return Service.START_STICKY;
    }

    @Override
    public void onDestroy() {
        DebugLog.v("onDestroy");
        unregisterReceiver(receiver);
        super.onDestroy();
    }

    private void startDownload() {
        mDownloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(APK_URL));
        request.setDescription("正在为您下载 友你 App的最新版本");
        request.setMimeType("application/vnd.android.package-archive");
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS+"/youni", APK_NAME);
        enqueue = mDownloadManager.enqueue(request);
    }
    /**
     * 递归删除文件和文件夹
     * @param file    要删除的根目录
     */
    public void RecursionDeleteFile(File file){
        if(file.isFile()){
            file.delete();
            return;
        }
        if(file.isDirectory()){
            File[] childFile = file.listFiles();
            if(childFile == null || childFile.length == 0){
                file.delete();
                return;
            }
            for(File f : childFile){
                RecursionDeleteFile(f);
            }
            file.delete();
        }
    }
}
然后再检测到服务器有新版本后,弹出对话框,询问用户是否下载

  /*系统提示对话框*/
new AlertDialog.Builder(NewMainActivity.this).setTitle("操作提示")//设置对话框标题
        .setMessage("检测到最新版本,是否要升级?")//设置显示的内容
        .setPositiveButton("后台下载",new DialogInterface.OnClickListener() {//添加确定按钮
            @Override
            public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
                startService(new Intent(getApplicationContext(), DownloadService.class));
            }
        }).setNegativeButton("下次再说",new DialogInterface.OnClickListener() {//添加返回按钮
    @Override
    public void onClick(DialogInterface dialog, int which) {//响应事件

    }
}).show();//在按键响应事件中显示此对话框
下载后就可以自动安装了


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值