Android Question How to remove foreground service icon for app with targetSdkVersion="14" ?

FrankBerra

Active Member
Licensed User
Longtime User
Hi everyone
today i compiled an app that i was not compiling it since 2 months ago.
The app has a service that runs about every 5 minutes and the service completes it's tasks in about 30 seconds.
The app is not ready to comply the new android 8 background service limitations but i need to update it to fix some bugs.
The problem is that now when compiled with B4A v.8.0 a persistent notification appears in the notification area even if i put Service.StopAutomaticForeground at the end of the service_start sub and even if i target the app to SdkVersion 14.

Meantime i adapt the app to the new google play rules, how can i continue to run the service in background without showing any icon?
Somewhere i read that Service.StopAutomaticForeground must be called within 5 seconds after the service is called...but my service takes up to 30 seconds to complete it's task. How can deal with this?

Thank you in advance for your support!
 

FrankBerra

Active Member
Licensed User
Longtime User
Well, in starter service i start my service with StartService(MyService) and in the code of MyService i restart it with:
B4X:
If p.SdkVersion < 23 Then
        StartServiceAtExact("", RestartTime, True)
      Else 'https://www.b4x.com/android/forum/threads/now-android-6-servicestartatexact-not-accurate.65117/#post-414796
        Dim srvAt As JavaObject
        'call the function
        srvAt.InitializeContext
        srvAt.RunMethod("StartServiceAtExactWhileIdle", Array(RestartTime, True))
 End If


#If JAVA
//import android.app.Service;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;

import android.provider.Settings;
import android.provider.Settings.Secure;

public void StartServiceAtExactWhileIdle(Long time, Boolean wakeUp){ 
   AlarmManager alMan = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
   Intent intent = new Intent(this.getApplicationContext(), this.getClass());
   PendingIntent penInt = PendingIntent.getService(this.getApplicationContext(), 1, intent, 134217728);
   alMan.setExactAndAllowWhileIdle(wakeUp?AlarmManager.RTC_WAKEUP:AlarmManager.RTC, time, penInt);
}

#End If

So, based on your suggestion should i set Service.AUTOMATIC_FOREGROUND_NEVER in service_create and then add something like:

B4X:
Sub Service_Start (StartingIntent As Intent)
Service.StartForeground(123, Notification)

<...code of the service...>

Service.StopForeground(123)
End sub

is it right?
 
Upvote 0
Top