• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

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

Adddeeee

Lurker
Dec 21, 2020
1
0
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?
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones