1. Are you ready for the Galaxy S20? Here is everything we know so far!

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

Discussion in 'Android Development' started by Adddeeee, Dec 21, 2020.

  1. Adddeeee

    Adddeeee Lurker
    Thread Starter

    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 service extension 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. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - Can't seem Android
  1. jimmygee
    Replies:
    1
    Views:
    155
  2. gillsman
    Replies:
    3
    Views:
    173
  3. claytoncarney
    Replies:
    0
    Views:
    217
  4. yossssssss
    Replies:
    0
    Views:
    153
  5. MyNeverZen
    Replies:
    6
    Views:
    398
  6. billdz
    Replies:
    3
    Views:
    360
  7. bertie11
    Replies:
    2
    Views:
    367
  8. sukhreetsingh
    Replies:
    8
    Views:
    459
  9. Hthorpe52
    Replies:
    1
    Views:
    732
  10. Jazmyn Ruud
    Replies:
    4
    Views:
    507

Share This Page

Loading...