Android Example Service.StartForeground API 24+

Hello All.

Just thought I would share some info with you. Most of our services in our apps would use
B4X:
Service.StartForeground(1,main_service.n1)

in the service create sub, where main_service is a long running background task, and has the notification object in it. This was used so android would not kill the service just launched if it was a long running service. Over the years methods have changed and API level have improved, so the app evolves, as it should.

We were testing on of our app on API 24 android 7 and noticed that when the phone was screen off and most probably sleeping when our service started, we use this code now
B4X:
Sub schedulecallback(timeToAlarm As Long)
   
   Dim p As Phone
   
   mod_functions.writelog("svc_ibeacon(), schedulecallback, API = " & p.SdkVersion)
   
   If p.SdkVersion >= 23 Then
     Dim srvAt As JavaObject
     srvAt.InitializeContext
     srvAt.RunMethod("StartServiceAtExactWhileIdle", Array(timeToAlarm, True))
   Else
     StartServiceAt("",timeToAlarm,True)
   End If

   
   
End Sub

#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;

   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

the screen would light up and then dim again. We did not see this on API 23 and below. So, after some further investigation it would appear that using the startforeground method under API 24 triggers a notification event to occur. @Erel could probably verify this.

Hope this info in of help.

Regards

John.
 

Jmu5667

Well-Known Member
Licensed User
Longtime User
Please check if there's no energy setting is stopping the service. On my Huawei P8 I had to set my app to "protected". Otherwise it was paused when turning off the screen.

I do not have such a issue, and do not fully understand what you mean.
 

db0070

Active Member
Licensed User
Longtime User
Hello All.

Just thought I would share some info with you. Most of our services in our apps would use
B4X:
Service.StartForeground(1,main_service.n1)

in the service create sub, where main_service is a long running background task, and has the notification object in it. This was used so android would not kill the service just launched if it was a long running service. Over the years methods have changed and API level have improved, so the app evolves, as it should.

We were testing on of our app on API 24 android 7 and noticed that when the phone was screen off and most probably sleeping when our service started, we use this code now
B4X:
Sub schedulecallback(timeToAlarm As Long)
  
   Dim p As Phone
  
   mod_functions.writelog("svc_ibeacon(), schedulecallback, API = " & p.SdkVersion)
  
   If p.SdkVersion >= 23 Then
     Dim srvAt As JavaObject
     srvAt.InitializeContext
     srvAt.RunMethod("StartServiceAtExactWhileIdle", Array(timeToAlarm, True))
   Else
     StartServiceAt("",timeToAlarm,True)
   End If

  
  
End Sub

#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;

   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

the screen would light up and then dim again. We did not see this on API 23 and below. So, after some further investigation it would appear that using the startforeground method under API 24 triggers a notification event to occur. @Erel could probably verify this.

Hope this info in of help.

Regards

John.

I am having trouble waking up my device (even after I have made it a sticky service) - using Erel's StartServiceAtExactWhileIdle, so while I understand this is essentially the same code as Erel's I am going to give this a go. One question, what and where is mod.functions defined? Do I need this, as it is only recording a log?
 

Jmu5667

Well-Known Member
Licensed User
Longtime User
I am having trouble waking up my device (even after I have made it a sticky service) - using Erel's StartServiceAtExactWhileIdle, so while I understand this is essentially the same code as Erel's I am going to give this a go. One question, what and where is mod.functions defined? Do I need this, as it is only recording a log?

Hi

Create new new module called mod_functions and put this in it.
B4X:
Sub writelog(m As String)
   
   Dim s As OutputStream, b() As Byte  
   Dim TimeFormat, myTimeFormat As String
   
   Try
     ' // save existing time format
     TimeFormat = DateTime.TimeFormat
         
     myTimeFormat = "HH:mm:ss.SSS"
     ' // set date time format
     DateTime.TimeFormat = myTimeFormat
     ' // debugger logging
     Log(DateTime.Date(DateTime.Now) & " " & DateTime.time(DateTime.Now) & " - " & m)
     ' // create log data for file
     m = DateTime.Date(DateTime.Now) & " " & DateTime.time(DateTime.Now) & " - " & m & Chr(10) & Chr(13)
     ' // restore date time format
     DateTime.TimeFormat = TimeFormat
     
     b = m.GetBytes("UTF8")
     
     If File.ExternalWritable = False Then
       If File.Size(File.DirInternal, "yourlogfilename.log") >= 8388608 Then
         File.Delete(File.DirInternal, "yourlogfilename.log")
       End If
       s = File.OpenOutput(File.DirInternal, "yourlogfilename.log", True)
       
     Else
       If File.Size(File.DirDefaultExternal, "yourlogfilename.log") >= 8388608 Then
         File.Delete(File.DirDefaultExternal, "yourlogfilename.log")
       End If
       s = File.OpenOutput(File.DirDefaultExternal,"yourlogfilename.log", True)
     End If
     
     s.WriteBytes(b,0,b.Length -1)
     s.Flush
     s.close

     
   Catch
     'ToastMessageShow("writelog, error - " & LastException.Message,True)
     Log("writelog, error - " & LastException.Message)
     Log("writelog, m =  " & m)
   End Try
     
   
End Sub


Regards

John.
 

db0070

Active Member
Licensed User
Longtime User
Well, that did not wake up my device - even if I set the alarm 2 minutes later from Now. I have gone back to Erel's code which works, but for some reason the alarm for next morning does not go off, whereas alarms set later on the same day will go off. The logs show the API = 23
 
Top