Important: Please read the Qt Code of Conduct -

Can't seem to make Android foreground service run all the time

  • Hello!

    I've had a big issue with the service running on my Android app. The app is connected to a websocket, which at certain events will send data to the users. If the app is not actively listening, it will lose data.

    For the above reason, I created a foreground service. However, it seems to be very unstable. As long as I have the power cord connected, it runs just fine, but as soon as I unplug it, it starts to (what I belive) pause the service.

    I have added the service in AndroidManifest:

            <service android:name=".QtAndroidService" android:stopWithTask="true">
                <!-- Background running -->
                <meta-data android:name="" android:value="true"/>
                <!-- Background running -->

    It is created within the application, in parallel with the activity.

    The QtAndroidService (extends Service) looks like this:

      public void onCreate() {
            Log.i(TAG, "Creating Service");
        public void onDestroy() {
            Log.i(TAG, "Destroying Service");
        public int onStartCommand(Intent intent, int flags, int startId) {
            NotificationChannel serviceChannel = new NotificationChannel(
                    "Example Service Channel",
            NotificationManager manager = getSystemService(NotificationManager.class);
            Intent notificationIntent = new Intent(this,;
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
            Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
            startForeground(1, notification);
            PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
            WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
            Timer timer = new Timer();
            TimerTask t = new TimerTask() {
                public void run() {
            return START_REDELIVER_INTENT;
        public IBinder onBind(Intent intent) {
            return null;

    I also added a wake lock of pure desperation. That does not solve the issue either....

    I'm not sure I've done everything according to what a foreground service requires, but I do get the foreground service notification showing.

    What am I missing getting this service to run forever?

Log in to reply