Android:onDestroy在调用stopSelf后未执行
我有一个QtAndroid应用程序,其中一个服务是使用QtService实现的。如果我想退出服务,我会这样做: C++中的:Android:onDestroy在调用stopSelf后未执行,android,qt,service,ondestroy,Android,Qt,Service,Ondestroy,我有一个QtAndroid应用程序,其中一个服务是使用QtService实现的。如果我想退出服务,我会这样做: C++中的: QtAndroid::androidService().callMethod<void>("quit"); public void quit() { stopSelf(); } 还有一个onDestroy函数: @Override public void onDestroy() { Log.d("MyService", "Called on
QtAndroid::androidService().callMethod<void>("quit");
public void quit() {
stopSelf();
}
还有一个onDestroy函数:
@Override
public void onDestroy() {
Log.d("MyService", "Called onDestroy");
super.onDestroy();
}
日志消息从未显示,我认为onDestroy函数没有被调用。在我的应用程序中,服务甚至没有停止。我尝试了一个基于。通过这个最小的实现,服务将停止,但onDestroy函数也不会被调用
为了演示这个问题,我使用了示例源代码,并对其进行了如下修改:
server.cpp
#include <QAndroidService>
#include <QtAndroid>
#include "rep_pingpong_source.h"
class PingPong : public PingPongSource {
public slots:
// PingPongSource interface
void ping(const QString &msg) override {
qDebug() << "KDAB: Message "+msg;
emit pong(msg + " from server");
if (msg == "Quit") {
qDebug() << "MyService: C++ quit";
QtAndroid::androidService().callMethod<void>("quit");
}
}
};
int main(int argc, char *argv[])
{
QAndroidService app(argc, argv);
QRemoteObjectHost srcNode(QUrl(QStringLiteral("local:replica")));
PingPong pingPongServer;
srcNode.enableRemoting(&pingPongServer);
return app.exec();
}
如果我查看日志,则会显示被调用的退出消息,但不会显示被调用的onDestroy。您知道为什么会发生这种情况,以及如何调试该行为吗?还有,我的服务不会停止的原因是什么?我怎样才能知道是什么阻止它退出
谢谢大家! android中的每项服务都可以作为前台或后台运行。如果你想退出服务。您只需要调用
stopSelf()代码>
onDestroy中的方法,但在调用stopSelf()之前,需要确保没有发生回调
调用stopService
并有意从活动或片段调用ondestory
方法
stopService(Intent(this, MyService.class))
stopService(Intent(this, MyService.class))