Android Question Curious things of Android

bgsoft

Well-Known Member
Licensed User
Longtime User
I have a program that calls a service. This service is called again from "Service_Start" with "StartServiceAt" with times exceeding eight hours. The service works perfectly.

If from the phone, press menu (hard button), I enter "Clear memory", and delete. If there are many background applications, Android puts service on the schedule, and this service is no longer visible in Settings / Application Manager / In progress, It does not kill the service, because when your time comes activates the service, the problem is that:

1) The variables declared in "Process_Globals" the reset.
2) Re-enter "Service_Create" when should enter "Service_Start", so the initialization code will execute again 3) After entering "Service_Create" enters "Service_Start".

Conclusion: The service continues and goes back into its proper time, but comes as if for the first time. If someone you do not expect that for the "Service_Create" initialized variables, do it again.
I this "problem" I have turned writing data to a file.

This I found out putting a trace to the application service and taking several days running.

Note: Sometimes, you do not need the user to clear the memory for this to happen, Android sometimes does with the same effect.

Regards
 

bgsoft

Well-Known Member
Licensed User
Longtime User
Yes, I read that tutorial, but do not talk about "Android Process and life cycle activities" if not "Service Modules", you've read this tutorial written by Mr. Erel? :D

http://www.b4x.com/android/forum/threads/service-modules.7542/

And specifically this section?:

Sub Service_Create is called when the service is first started. This is the place to initialize and set the process global
variables. Once a service is started it stays alive until you call StopService or until the whole process is destroyed.


And you read my post specifically in the section?:

I have a program that calls a service. This service is called again from "Service_Start" with "StartServiceAt" with times
exceeding eight hours. The service works perfectly.
If from the phone, press menu (hard button), I enter "Clear memory", and delete. If there are many background applications, Android puts service on the schedule, and this service is no longer visible in Settings / Application Manager/ In progress, It does not kill the service, because when your time comes activates the service, the problem is that:

1) The variables declared in "Process_Globals" the reset.
2) Re-enter "Service_Create" when should enter "Service_Start", so the initialization code will execute again
3) After entering "Service_Create" enters "Service_Start".

And do not talk about B4A, if not a "problem" that has Android as well say in my head "Curious things of Android" It is
normal that if I delete the memory (as commented above) Android does not kill the service, but if between his time but with all reset and re-enter "Service_Create"?
In your tutorial (I read) you put:

Sub Service_Create is called when the service is first started. This is the place to initialize and set the process global
variables. Once a service is started it stays alive until you call StopService or until the whole process is destroyed.


I give as true that that is so, until putting a trace in my program I see that does not behave well when memory is erased. Erel, this is not a critique of your tutorial or B4A, I only intend to share it with the forum, if this happens to someone, can go crazy to know that the problem is not the program, the problem is how Android manages the services.

It's easy to find out, you put a trace to write to a file the date and time that goes into "Service_Create" and
"Service_Start". It creates a global variable and is given a value, and before its value is written in the trace. It runs the service with a long time (hours), this active look "running programs" open applications and can not be killed, and then clear the memory. If the service disappeared from "running programs", you can see that the next time you enter the service, global variables are resetted and re-enters "Service_Create"

I do not think this one pass me, so I wanted to share with the forum.

And this I've tried in various terminals with different versions of Android, and it always happens.
In no time I have tried to comment lightly. This comment is based on many hours lost until finally saw the problem. And this problem I wanted to comment in the forum, because neither is a program problem, or B4A, it is as I said, of how Android manages the services. And if anyone wants to see that so is simply to make the trace I have indicated. That if the problem is likely not going to go to the first, and for that reason have to spend many hours as I lost.


regards

 
Upvote 0

bgsoft

Well-Known Member
Licensed User
Longtime User
Hi Erel, thanks for your interest

I created a small program to prove my theory (I attached).
I've run, I erased the memory, and rarely do the test has failed.
I am attaching the trace file where you see that when I had to re-enter, but Service Create enters. When you clear memory running apps did not appear.

As I said, already solved the problem. I did not want a solution, if not to share with the forum.

Text Trace:

10/10/2013 - 13:12:04 ----- Service_Create ------
10/10/2013 - 13:12:04 Flag_ON before giving value false
10/10/2013 - 13:12:04 NextTime before giving value 0
10/10/2013 - 13:12:04 ----- Service_Start IN -----
10/10/2013 - 13:12:04 Time To alarm: 13:15
10/10/2013 - 13:12:04 NextTime in Ticks 1381403700000
10/10/2013 - 13:12:04 NextTime 10/10/2013 - 13:15:00

10/10/2013 - 13:12:04 Flag_ON true
10/10/2013 - 13:12:04 ----- Service_Start OUT -----


******* At this time I reset the memory ********

Enter your time but with variables resetted and again in the Service create
10/10/2013 - 13:15:00 ----- Service_Create ------
10/10/2013 - 13:15:00 Flag_ON before giving value false
10/10/2013 - 13:15:00 NextTime before giving value 0

10/10/2013 - 13:15:00 ----- Service_Start IN -----
10/10/2013 - 13:15:00 Time To alarm: 13:15
10/10/2013 - 13:15:00 NextTime in Ticks 1381490100000
10/10/2013 - 13:15:00 NextTime 10/11/2013 - 13:15:00 <- Here adds 1 day after recalculating

10/10/2013 - 13:15:00 Flag_ON true
10/10/2013 - 13:15:00 ----- Service_Start OUT -----




regards
 

Attachments

  • Pruebas Servicios.zip
    378.5 KB · Views: 305
  • TrazaBGS.txt
    895 bytes · Views: 256
Upvote 0

corwin42

Expert
Licensed User
Longtime User
I don't see any problem here. It is just the normal Android process life cycle.

Is your "reset/clear memory" done with a task killer?

If you start a service with StartServiceAt() only an alarm is added to the system that your service should be started at the given time.
When the alarm fires there are two possibilities then:
1. Your process is still alive which means only Service_Start is called and your process_globals are still valid.
2. Your process was killed from the system which means process_globals is cleared and Service_Create is called where you should reinitialize your process_globals values.
Your app should always handle both situations.

There is a strange thing in your code that will cause problems. In your main code you call:
B4X:
StartService(ServicioPruebas)
ExitApplication

This don't make sense. StartService will only be called when the program is ready to process messages. But with ExitApplication you kill the process before the message queue can be handled. Never ever use ExitApplication except you know exactly what you are doing. In 99,999% of all apps ExitApplication is not needed and will just cause problems!
 
Upvote 0

bgsoft

Well-Known Member
Licensed User
Longtime User
First of all thank you for your clarification, but I think you have not read my post well.

I don't see any problem here. It is just the normal Android process life cycle.

To my knowledge, the life cycle does not kill services if applications in the background and rarely in the foreground, you can read the tutorials that are on it, for example here.

http://www.b4x.com/android/forum/threads/service-modules.7542/

It is important to understand how Android chooses which process to kill when it is low on memory (a new process will later be created as needed).
A process can be in one of the three following states:
- Foreground - The user currently sees one of the process activities.
- Background - None of the activities of the process are visible, however there is a started service.
- Paused - There are no visible activities and no started services.



Sub Service_Create is called when the service is first started. This is the place to initialize and set the process global variables. Once a service is started it stays alive until you call StopService or until the whole process is destroyed.

Is your "reset/clear memory" done with a task killer?

I do not kill the task, that makes ExitApplication

If you start a service with StartServiceAt() only an alarm is added to the system that your service should be started at the given time.
When the alarm fires there are two possibilities then:
1. Your process is still alive which means only Service_Start is called and your process_globals are still valid.
2. Your process was killed from the system which means process_globals is cleared and Service_Create is called where you should reinitialize your process_globals values.


All that you tell me I already know, and you again insist that the system does not kill the Service, just reset the variables and re-enters Service create. If you look at the trace'll see.


Your app should always handle both situations.

If you had read my post you'd know that I have no problem with this, that my app already provides these two possibilities (and many more), I put this post to share with the rest of the forum and I saw something curious.
You can read it above:

And this I've tried in various terminals with different versions of Android, and it always happens.

In no time I have tried to comment lightly. This comment is based on many hours lost until finally saw the problem. And this problem I wanted to comment in the forum, because neither is a program problem, or B4A, it is as I said, of how Android manages the services. And if anyone wants to see that so is simply to make the trace I have indicated. That if the problem is likely not going to go to the first, and for that reason have to spend many hours as I lost.


This don't make sense. StartService will only be called when the program is ready to process messages. But with ExitApplication you kill the process before the message queue can be handled. Never ever use ExitApplication except you know exactly what you are doing. In 99,999% of all apps ExitApplication is not needed and will just cause problems!

The 100,000000% of times I have called :

StartService (ServicioPruebas)
ExitApplication


The service works , there has not been one single time not to go, because if I had seen so outside in the log , trace , etc, etc, etc.
On the other hand , as I said at the beginning, this is a little test I did at one moment Erel request , and the important thing is not color of buttons or as the program is done , the important thing is that when I make a clear memory from the phone , sometimes , not always, reset the variables , but DOES NOT KILL THE SERVICE. If there is a problem with the ExitApplication , he had seen in the log and trace, and as I say , at no time did.

As we have the example , change it for a Activity.Finish, memory load with various applications, and then erases the memory , and checks if it exists in "running processes" , and if not, possibly see what I 'm talking about consulting the log or trace. It is normal to not kill the service (at no time out "Service_Destroy" ) but to re-enter Service Create? I think not , and have not read in any place where I say that without destroying or stopping the service must reenter Service Create

Vielen Dank

Regards
 
Upvote 0

bgsoft

Well-Known Member
Licensed User
Longtime User
Hi Erel

Have you tried the little program I did?

This week I have continued to test back. And has been passed from time to time. The thing that surprises me is that the service does not kill, but resets the variables and re-enter Service Create.

As I told you, what I have resolved the problem. I have great interest in knowing your opinion.

regards
 
Upvote 0

bgsoft

Well-Known Member
Licensed User
Longtime User
Thanks for your answer.

As I told Corwin, this is not the problem, first: in my program that I have not put ExitApplication. And most importantly, in the example program, if you put Activity.Finish, instead of ExitApplication, the problem still exists.
I told Corwin also, so expect your answer, because the problem is not the ExitApplication.

regards
 
Upvote 0

bgsoft

Well-Known Member
Licensed User
Longtime User
The problem is that the variables are deleted, and enters "Service Create" and does not kill the service.
And you're right, never enters the trace of Service Destroy

I don't see anything strange in what you posted.

At least we assume that the test program is well.Now we have to know, that resets the variables
 
Last edited:
Upvote 0

bgsoft

Well-Known Member
Licensed User
Longtime User
Doubts after your comment:

1) If the process is destroyed, who calls him back?
2) If the process is destroyed, that goes back into time "accurate" was called before destroying?.
3) If the process is destroyed, why not enters "Sub Service_Destroy" as you say in your tutorial?
4) Because if I stop the process, or kill from the "Application Manager", it goes into "Sub Service_Destroy"? This contradicts your comment.


regards
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1) If you called ExitApplication then Android may recreate the process right after the call as if the process crashed. Calling StartServiceAt will cause the process to start if it is not running (in the scheduled time).

2) I'm sorry but I'm not going to delve into every detail of your application.
3) Where did I say so?
I only found this line in the services tutorial:
Sub Service_Destroy is called when you call StopService.
4) The correct statement is that there is no guarantee that Service_Destroy will be raised when the process is killed. The same is correct for Activity_Pause.

If you are not satisfied with this explanation then you can read more about services here: http://developer.android.com/guide/components/services.html
This is not a B4A concept. It is a feature of the OS.
 
Upvote 0

bgsoft

Well-Known Member
Licensed User
Longtime User
Hi

1) If you called ExitApplication then Android may recreate the process right after the call as if the process crashed. Calling StartServiceAt will cause the process to start if it is not running (in the scheduled time).
So the comments (of more top) that should not be used ExitAplication denies this.


2) I'm sorry but I'm not going to delve into every detail of your application.
The number 1) and answer the 2). But also it was you who asked me a sample program.
Can you upload a small program that demonstrates it?


4) The correct statement is that there is no guarantee that Service_Destroy will be raised when the process is killed. The same is correct for Activity_Pause.
If you at first told me this, I keep asking, with this response is all said and clarified.


This is not a B4A concept. It is a feature of the OS.
I said this in the beginning and repeatedly.
And do not talk about B4A, if not a "problem" that has Android as well say in my head "Curious things of Android"


Thank you very much for the address where extensively explains the operation of the services:
http://developer.android.com/guide/components/services.html

Sometimes I think it is best to redirect the questioner to these sites, to enter into discussions that sometimes lead nowhere.
As a beginning in B4A sometimes do not know where I can find this type of information (And a lot more people).

I think it was interesting that in their tutorial: "Service Modules", added paragraph 4, if I came to read that, did not raise this question.

Thanks as always for your contribution.

regards
 
Upvote 0
Top