summaryrefslogtreecommitdiffstats
path: root/src/android/jar/src/org/qtproject/qt/android/QtServiceBase.java
blob: b69dea441664a1c22e56c12208fda7ddb8f6338e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

package org.qtproject.qt.android;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class QtServiceBase extends Service {
    @Override
    public void onCreate()
    {
        super.onCreate();

        // the application has already started, do not reload everything again
        if (QtNative.getStateDetails().isStarted) {
            Log.w(QtNative.QtTAG,
                    "A QtService tried to start in the same process as an initiated " +
                            "QtActivity. That is not supported. This results in the service " +
                            "functioning as an Android Service detached from Qt.");
            return;
        }

        QtNative.setService(this);

        QtServiceLoader loader = new QtServiceLoader(this);
        loader.loadQtLibraries();
        QtNative.startApplication(loader.getApplicationParameters(), loader.getMainLibraryPath());
        QtNative.setApplicationState(QtNative.ApplicationState.ApplicationHidden);
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        QtNative.quitQtCoreApplication();
        QtNative.terminateQt();
        QtNative.setService(null);
        QtNative.getQtThread().exit();
        System.exit(0);
    }

    @Override
    public IBinder onBind(Intent intent) {
        synchronized (this) {
            return QtNative.onBind(intent);
        }
    }
}