|
自己VS2008编译的4.5.1版本,感觉极其的不准啊,有时候能读出输出,有时候不能,还是我代码有问题? ps:adb.exe是Android SDK里的东东,命令行下使用正常。 - #ifndef HIADBWRAPPER_H
- #define HIADBWRAPPER_H
- #include <QProcess>
- #include <QByteArray>
- #include <QString>
- #include <QStringList>
- class HiAdbWrapper: public QObject
- {
- Q_OBJECT
- public:
- HiAdbWrapper(QObject* parent = 0);
- virtual ~HiAdbWrapper();
- public:
- QStringList devices();
- QString runCommand(const QString& device, const QString& command);
- bool installApp(const QString& device, const QString& path);
- bool removeApp(const QString& device, const QString& package);
- bool downloadFile(const QString& device, const QString& remote, const QString& local);
- bool uploadFile(const QString& device, const QString& local, const QString& remote);
-
- protected slots:
- void readStdOut();
- void readStdErr();
- private:
- QProcess* theProcess;
- QString stdOut;
- QString stdErr;
- };
- #endif
- #include "hiadbwrapper.h"
- HiAdbWrapper::HiAdbWrapper(QObject* parent): QObject(parent)
- {
- theProcess = new QProcess(this);
- connect(theProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(readStdOut()));
- connect(theProcess, SIGNAL(readyReadStandardError()), this, SLOT(readStdErr()));
- }
- HiAdbWrapper::~HiAdbWrapper()
- {
- }
- QStringList HiAdbWrapper::devices()
- {
- stdOut = QString::null;
- stdErr = QString::null;
- QStringList args;
- QStringList deviceList;
- args<<"devices";
- theProcess->start(QString("res\\bin\\adb.exe"), args);
- theProcess->waitForFinished();
- if(stdErr != QString::null)
- return deviceList;
- deviceList = stdOut.split(QChar('\n'));
- deviceList.removeAt(0);
- for(int i = deviceList.count()-1; i >= 0; i--)
- {
- deviceList[i].replace(QRegExp("\t.+[ wind_phpcode_2 ]quot;), QString::null);
- deviceList[i] = deviceList[i].trimmed();
- if(deviceList[i].isEmpty())
- deviceList.removeAt(i);
- }
- return deviceList;
- }
- QString HiAdbWrapper::runCommand(const QString& device, const QString& command)
- {
- stdOut = QString::null;
- stdErr = QString::null;
- QStringList args;
- args<<"-s"<<device<<"shell"<<command;
- theProcess->start(QString("res\\bin\\adb.exe"), args);
- theProcess->waitForFinished();
- if(stdErr != QString::null)
- return stdErr;
- else
- return stdOut;
- }
- bool HiAdbWrapper::installApp(const QString& device, const QString& path)
- {
- stdOut = QString::null;
- stdErr = QString::null;
- QStringList args;
- args<<"-s"<<device<<"install"<<"-r"<<path;
- theProcess->start(QString("res\\bin\\adb.exe"), args);
- theProcess->waitForFinished();
- if(stdErr != QString::null)
- return false;
- else
- {
- if(stdOut.indexOf(QString("Success")) != -1)
- return true;
- else
- return false;
- }
- }
- bool HiAdbWrapper::removeApp(const QString& device, const QString& package)
- {
- stdOut = QString::null;
- stdErr = QString::null;
- QStringList args;
- args<<"-s"<<device<<"uninstall"<<package;
- theProcess->start(QString("res\\bin\\adb.exe"), args);
- theProcess->waitForFinished();
- if(stdErr != QString::null)
- return false;
- else
- {
- if(stdOut.indexOf(QString("Success")) != -1)
- return true;
- else
- return false;
- }
- }
- bool HiAdbWrapper::downloadFile(const QString& device, const QString& remote, const QString& local)
- {
- stdOut = QString::null;
- stdErr = QString::null;
- QStringList args;
- args<<"-s"<<device<<"pull"<<remote<<local;
- theProcess->start(QString("res\\bin\\adb.exe"), args);
- theProcess->waitForFinished();
- if((stdOut.indexOf(QString("KB/s")) != -1) || (stdErr.indexOf(QString("KB/s")) != -1))
- return true;
- else
- return false;
- }
- bool HiAdbWrapper::uploadFile(const QString& device, const QString& local, const QString& remote)
- {
- stdOut = QString::null;
- stdErr = QString::null;
- QStringList args;
- args<<"-s"<<device<<"push"<<local<<remote;
- theProcess->start(QString("res\\bin\\adb.exe"), args);
- theProcess->waitForFinished();
- if((stdOut.indexOf(QString("KB/s")) != -1) || (stdErr.indexOf(QString("KB/s")) != -1))
- return true;
- else
- return false;
- }
- void HiAdbWrapper::readStdOut()
- {
- stdOut += theProcess->readAllStandardOutput();
- }
- void HiAdbWrapper::readStdErr()
- {
- stdErr += theProcess->readAllStandardError();
- }
|