Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Can't seem to make Android foreground service run all the time
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
1 Posts 1 Posters 601 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    adddeeee
    wrote on last edited by adddeeee
    #1

    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.app.background_running" android:value="true"/>
                <!-- Background running -->
            </service>
    

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

    The QtAndroidService (extends Service) looks like this:

      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) {
            NotificationChannel serviceChannel = new NotificationChannel(
                    CHANNEL_ID,
                    "Example Service Channel",
                    NotificationManager.IMPORTANCE_NONE
            );
    
            serviceChannel.setShowBadge(false);
    
            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(serviceChannel);
    
            Intent notificationIntent = new Intent(this, org.qtproject.qt5.android.bindings.QtActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
            Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setContentTitle("Stryket")
                    .setSmallIcon(R.drawable.icon)
                    .setContentIntent(pendingIntent)
                    .build();
            startForeground(1, notification);
    
            PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
            WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                    "MyApp::MyWakelockTag");
    
            wakeLock.acquire();
    
            Timer timer = new Timer();
    
            TimerTask t = new TimerTask() {
                @Override
                public void run() {
                   DO_SOME_STUFF_WHILE_SERVICE_IS_RUNNING_FOREVER();
                }
            };
            timer.scheduleAtFixedRate(t,0,10000);
    
            return START_REDELIVER_INTENT;
        }
    
        @Override
        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?

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved