新的需求出来啦,需要自动更新功能,不怕程序升级了。

自动更新,QML不好写,需要c++来辅助,这里就涉及QML中调用c++功能(这里就不写了,百度一下,很多)。

思路:获取版本》下载程序》安装程序

第一步,获取版本信息,可以通过XMLHttpRequest获取最新版本。

第二步,下载程序,通过Qt c++的QtNetwork:

头文件:

 #ifndef ZRDOWNLOAD_H
#define ZRDOWNLOAD_H #include <QObject>
#include <QtNetwork/QNetworkAccessManager>
#include <QFile>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkRequest>
#include <QIODevice> class ZRDownload : public QObject
{
Q_OBJECT
Q_PROPERTY(QString url READ GetUrl WRITE SetUrl)
Q_PROPERTY(QString fileName READ GetFileName WRITE SetFileName)
public:
explicit ZRDownload(QObject *parent = );
~ZRDownload(); QString GetUrl();
void SetUrl(QString u);
QString GetFileName();
void SetFileName(QString fn);
private:
QString url;
QString fileName;
QFile *file;
QNetworkAccessManager *accessManager;
QNetworkRequest request;
QNetworkReply *reply; signals:
void progressPosition(double pre); //更新进度条信号
void downloadFinished(); //结束信号 protected slots:
void httpDownload();
void replyFinished(QNetworkReply*reply);
void onDownloadProgress(qint64 bytesSent,qint64 bytesTotal);
void onReadyRead();
}; #endif // ZRDOWNLOAD_H

CPP文件:

#include "zrdownload.h"
#include <QDebug> ZRDownload::ZRDownload(QObject *parent) : QObject(parent)
{
accessManager = NULL;
reply = NULL;
}
ZRDownload::~ZRDownload()
{
} QString ZRDownload::GetUrl()
{
return url;
} void ZRDownload::SetUrl(QString u)
{
url = u;
} QString ZRDownload::GetFileName()
{
return fileName;
} void ZRDownload::SetFileName(QString fn)
{
fileName = fn;
} void ZRDownload::httpDownload(){ file=new QFile(fileName);
bool suc = file->open(QIODevice::WriteOnly);//只读方式打开文件 accessManager=new QNetworkAccessManager(this); request.setUrl(url); /******************设置http的header***********************/
// request.setHeader(QNetworkRequest::ContentTypeHeader, "multipart/form-data");
// request.setHeader(QNetworkRequest::ContentTypeHeader, "application/octet-stream");
// request.setRawHeader("Content-Disposition","form-data;name='doc';filename='a.txt'");
//request.setHeader(QNetworkRequest::ContentLengthHeader,post_data.length()); reply=accessManager->get(request);//通过发送数据,返回值保存在reply指针里.
connect(accessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));//finish为manager自带的信号,replyFinished是自定义的
connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT( onDownloadProgress(qint64 ,qint64 )));//download文件时进度
connect((QObject *)reply, SIGNAL(readyRead()),this, SLOT(onReadyRead())); }
/***************响应结束**************************/
void ZRDownload::replyFinished(QNetworkReply *reply){
//获取响应的信息,状态码为200表示正常
QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
QByteArray bytes = reply->readAll(); //获取字节
QString result(bytes); //转化为字符串 //无错误返回
if(reply->error() == QNetworkReply::NoError)
{
QByteArray bytes = reply->readAll(); //获取字节
QString result(bytes); //转化为字符串 file->flush();
file->close();
delete file;
file=NULL;
}
else
{
//处理错误
} reply->deleteLater();//要删除reply,但是不能在repyfinished里直接delete,要调用deletelater;
reply=NULL; accessManager->deleteLater();
accessManager=NULL; downloadFinished();
} /***********更新进度条*************/
void ZRDownload::onDownloadProgress(qint64 bytesSent, qint64 bytesTotal){ double pre = double(bytesSent)/bytesTotal; progressPosition(pre);
} void ZRDownload::onReadyRead(){ file->write(reply->readAll());
}

上面的代码也是修改网上的代码,增加了QML调用的信息。

注意:项目文件pro中需要添加 QT += network

main.cpp:

添加 qmlRegisterType<ZRDownload>("xxx.xxx.ZRDownload", 1, 0, "ZRDownload");

QML调用:

 ZRDownload{
id:download
url:"http://xxx.xxx.com/xx.apk"
fileName:""
onProgressPosition:progressBar.update(pre) //自定义更新进度条函数
onDownloadFinished: {
//下载完成
}
} //启动下载
download.fileName=getAppPath();//设置保存目录,pc和安卓不同
download.httpDownload();

第三步,安装程序:

不同平台,安装程序的代码也不同,PC平台使用QProcess启动安装程序,安卓平台通过QAndroidJniObject调用Activity实现。下面是代码:

 #ifndef ZRINSTALL_H
#define ZRINSTALL_H #include <QObject>
#include <QString> class ZRInstall : public QObject
{
Q_OBJECT
Q_PROPERTY(QString fileName WRITE SetFileName)
public:
explicit ZRInstall(QObject *parent = ); void SetFileName(QString fn);
signals: public slots:
void install();
private:
QString m_FileName;
}; #endif // ZRINSTALL_H

头文件

 #include "zrinstall.h"
#include <QDebug>
#if defined(Q_OS_ANDROID)
#include <QtAndroid>
#else
#include <QProcess>
#endif ZRInstall::ZRInstall(QObject *parent) : QObject(parent)
{ } void ZRInstall::SetFileName(QString fn)
{
m_FileName = fn;
} void ZRInstall::install()
{
#if defined(Q_OS_ANDROID)
//安装apk
QAndroidJniObject path = QAndroidJniObject::fromString("file://"+m_FileName);
QAndroidJniObject uri = QAndroidJniObject::callStaticObjectMethod("android/net/Uri", "parse", "(Ljava/lang/String;)Landroid/net/Uri;", path.object<jstring>());
QAndroidJniObject param = QAndroidJniObject::getStaticObjectField<jstring>("android/content/Intent", "ACTION_VIEW");
QAndroidJniObject intent("android/content/Intent","(Ljava/lang/String;)V",param.object<jstring>());
QAndroidJniObject type = QAndroidJniObject::fromString("application/vnd.android.package-archive");
QAndroidJniObject result = intent.callObjectMethod("setDataAndTypeAndNormalize", "(Landroid/net/Uri;Ljava/lang/String;)Landroid/content/Intent;", uri.object<jobject>(), type.object<jstring>());
QtAndroid::startActivity(intent, , );
#else
//qDebug()<<m_FileName;
QProcess * proc = new QProcess();
proc->startDetached(m_FileName,QStringList());
delete proc;
#endif
}

CPP文件

 android{
QT += androidextras
}

项目文件pro

调用就不写了,可以参考上面download。

这样整个版本自动更新就写好了,有一些细节自己需要注意,如下载安装文件不同,安装目录不同。

05-11 04:48