Service Module limit?

rfresh

Well-Known Member
Licensed User
Longtime User
I would like to run this code from my Service module to dim the display. It works fine in my Main activity but the p.SetScreenBrightness(14/255) chokes in the Service module. I'm trying to dim the whole phone display, not just while my app is running. I can do this from an activity just fine but the Service module doesn't seem to like it. Is there any way around this? Thanks...

B4X:
Dim p As Phone

WriteSetting("screen_brightness", 14)
p.SetScreenBrightness(14/255)

Sub WriteSetting(Setting As String, Value As Int)
    Dim r1 As Reflector
    Dim args(3) As Object
    Dim types(3) As String
    r1.Target = r1.GetContext
    args(0) = r1.RunMethod("getContentResolver")
    types(0) = "android.content.ContentResolver"
    args(1) = Setting
    types(1) = "java.lang.String"
    args(2) = Value
    types(2) = "java.lang.int"
    r1.RunStaticMethod("android.provider.Settings$System", "putInt", args, types) 
End Sub
 

corwin42

Expert
Licensed User
Longtime User
Setting screen brightness from a service is not so easy.

The WriteSetting sub changes the brightness in the android settings but does not change the current brightness.

Phone.SetScreenBrightness does not work from a service because it needs an activity.

The only solution I have found is to create a transparent activity that just calls Phone.SetScreenBrightness and then closes again. (You can google for that and will find this as the one and only solution)

So you have to:
1. Change phone brightness in settings with the WriteSetting sub
2. Start an invisible transparent activity that uses Phone.SetScreenBrightness
3. Immediately close the transparent activity with Activity.Finish.

BTW: In your example you set the brightness with both methods to 14. Be aware that WriteSetting sub uses a value range of 0-255 and Phone.SetScreenBrightness uses a value range of 0-100. So a fixed value of 14 is different screen brightness for these methods.
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
BTW when using a Service module, do I have to also use a keep alive statement? My service module connects to my website via FTP and downloads a small text file very 15 minutes and then it uses the email address in that file and sends an email to myself.

I leave the phone in my office and when I'm out, it stops sending emails. I leave the phone on but don't keep the app open and active as the focused app. My android home screen shows but I press the power button to turn off the display (not power down the phone). I think my phone is going to sleep and the service module isn't able to keep running?
 
Upvote 0

rbsoft

Active Member
Licensed User
Longtime User
In my app I have a service that keeps a TcpIp communication running even when the phone's screen is off. To obtain that I had to use the following:

Start the service as forground.
Use a notification.
Set PhoneWakestate to PartialLock to keep the processor running.

That drains the battery usually in about 4 to 5 hours. But that does not matter with my customers, since the devices are mostly mounted to the dashboard and are charged.


Rolf
 
Upvote 0

corwin42

Expert
Licensed User
Longtime User
BTW when using a Service module, do I have to also use a keep alive statement? My service module connects to my website via FTP and downloads a small text file very 15 minutes and then it uses the email address in that file and sends an email to myself.

How do you start the download every 15 minutes? You should not use a timer in a service because you can not be shure that your service will not be killed by the system. Reschedule your service with StartServiceAt every time.
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
>Reschedule your service with StartServiceAt every time.

Hmmm...yes, that's what I'm doing now. I don't use a Timer.

>In my app I have a service that keeps a TcpIp communication running even when the phone's screen is off.

Is that what the problem is? When my phone screen goes off, the Service module stops working?
 
Upvote 0

rbsoft

Active Member
Licensed User
Longtime User
That's what my problem was that the service stopped after some time (hours sometimes). My app is sending status messages every 15 to 60 seconds. That is why I used the partiallock. Of course with the battery drain.

Rolf
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Do you set it to run during sleep?
StartServiceAt (Service As Object, Time As Long, DuringSleep As Boolean)

Damn, I was hoping that was it, but yes I do set it to run during sleep:

B4X:
StartServiceAt("FTP",DateTime.Now,True)

I use the .Now to run it immediately out of my main Resume and then in the Service module, StartService I run it like this:

B4X:
StartServiceAt("",DateTime.Now + 900 * 1000,True)'every 15 mins

Hmmm...I guess I can try the partial lock but I don't think the battery drain will work that well for me...I'm not connected to a charger all the time.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
So far I havent needed to resort to Partial Lock and have been scheduling services for a while.
The main reason my service stopped running was because the scheduler was inside an IF statement for e.g.

B4X:
if (hello = 1) then
  scheduleservice
end if

Now, initially the app sets hello to 1,
but once the service gets destroyed later on due to low resources,
and the service schedules itself and wakes up,
the variable hello has been destroyed, and now contains 0,
so it will not schedule the service again.

so make sure the scheduler for your service is not dependent on any variables that might have been destroyed.

Just to make sure, I created a DebugLogger that logs when the service starts and when it schedules again. This helps a lot in debugging.
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Hmmm...good clue there. I don't run it out of an IF statement but the interval value is kept in a variable in my Main module and that might be dying and thus loosing the variable value. I will try moving the variable into my Service module (I wanted to be able to change the interval).

Good clue I think...thanks...
 
Upvote 0
Top