I have several problems with Qt Android services.
- The service isnt starting immediately. It runs after 1-2 minutes delay.
- The service stops after some time and the app crashes.
- QTimer in the service doesn't work.
- QTcpSocket client doesn't work. ( I added android.permission.INTERNET and still not works )
Im using the same main.cpp file for the Activity and Service. and this is the main function of the service::
QTcpSocket _socket;
void onReadyRead()
{
QByteArray datas = _socket.readAll();
QString DataAsString = QString(datas);
Log::log("DATA: ");
Log::log(DataAsString);
if(DataAsString!="HELLO\n")
NotificationClient().setNotification(DataAsString + " BGN");
}
int main_service(int argc, char *argv[])
{
QAndroidService app(argc, argv);
_socket.connectToHost(QHostAddress("xx.xx.xx.xx"), 1234);
QObject::connect(&_socket, &QTcpSocket::readyRead, onReadyRead);
_socket.write(QByteArray("get_money()\r\n"));
QThread::msleep(1000);
Log::log("creating timer..");
QTimer timer;
QObject::connect(&timer, &QTimer::timeout, someFunction);
timer.start(500);
//MoneyWorker *mw = new MoneyWorker();
for(;;)
{
_socket.write(QByteArray("get_money()\r\n"));
Log::log("loopin..");
QThread::msleep(10000);
}
//NotificationClient().setNotification("The user is happy!");
return app.exec();
}
Im starting the service this way::
QJniObject::callStaticMethod<void>(
"org/qtproject/example/qtandroidservice/QtAndroidService",
"startQtAndroidService",
"(Landroid/content/Context;)V",
QNativeInterface::QAndroidApplication::context());
package org.qtproject.example.qtandroidservice;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import org.qtproject.qt.android.bindings.QtService;
public class QtAndroidService extends QtService
{
private static final String TAG = "QtAndroidService";
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "Creating Service");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "Destroying Service");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int ret = super.onStartCommand(intent, flags, startId);
// Do some work
return ret;
}
public static void startQtAndroidService(Context context) {
context.startService(new Intent(context, QtAndroidService.class));
}
public static void stopQtAndroidService(Context context) {
context.stopService(new Intent(context, QtAndroidService.class));
}
public static void log(String message)
{
Log.i(TAG, message);
}
}
<service android:name="org.qtproject.example.qtandroidservice.QtAndroidService">
The goal is to show my debit card balance on my smart watch via notification so I can know when my money go low.
This is the log from 'adb logcat'
08-07 22:45:15.182 5342 5366 W ActivityManager: Timeout executing service: ServiceRecord{d2392a2 u0 org.qtproject.example.androidnotifier/org.qtproject.example.qtandroidservice.QtAndroidService}
08-07 22:45:15.531 5342 27607 E ActivityManager: Reason: executing service org.qtproject.example.androidnotifier/org.qtproject.example.qtandroidservice.QtAndroidService
08-07 22:47:35.784 5342 5366 W ActivityManager: Timeout executing service: ServiceRecord{33bcd86 u0 org.qtproject.example.androidnotifier/org.qtproject.example.qtandroidservice.QtAndroidService}
08-07 22:47:37.037 5342 29382 E ActivityManager: Reason: executing service org.qtproject.example.androidnotifier/org.qtproject.example.qtandroidservice.QtAndroidService
08-07 22:47:37.121 5342 7675 W ActivityManager: Scheduling restart of crashed service org.qtproject.example.androidnotifier/org.qtproject.example.qtandroidservice.QtAndroidService in 62676ms for start-requested
08-07 22:48:39.837 5342 5367 I ActivityManager: Start proc 29406:org.qtproject.example.androidnotifier/u0a477 for service {org.qtproject.example.androidnotifier/org.qtproject.example.qtandroidservice.QtAndroidService}
08-07 22:48:39.982 5342 7678 W ActivityManager: Stopping service due to app idle: u0a477 -1m34s200ms org.qtproject.example.androidnotifier/org.qtproject.example.qtandroidservice.QtAndroidService
08-07 22:48:40.198 29406 29428 I QtAndroidService: creating timer..
08-07 22:48:40.198 29406 29428 I QtAndroidService: loopin..
08-07 22:48:41.199 29406 29428 I QtAndroidService: loopin..
08-07 22:48:42.200 29406 29428 I QtAndroidService: loopin..
08-07 22:48:43.201 29406 29428 I QtAndroidService: loopin..
08-07 22:48:44.201 29406 29428 I QtAndroidService: loopin..
08-07 22:48:45.202 29406 29428 I QtAndroidService: loopin..
08-07 22:48:46.203 29406 29428 I QtAndroidService: loopin..
08-07 22:48:47.204 29406 29428 I QtAndroidService: loopin..
1.The service isnt starting immediately. It runs after 1-2 minutes delay.
I decided to go the Java way and do the task in java and I managed to succeed, now Im watching my money on my smart watch hehe :)
It uses node.js API with tcp server and puppeteer (headless chrome) that logs into my e-banking and gets my balance, listens to port and gives it on request.
and this is my java service:
this is my Kesh Class
and my TcpClient class