Android Question Suggestions needed about Service

ducphu

Active Member
Licensed User
Longtime User
I need your suggestions. I'm making a game and I want to have "life" is added every hour (like Candy Crush, Farm Hero etc...) I'm confusing and not sure how to do this. I'm thinking of using service module and StartServiceAt. But, what happens if my app is killed by the phone or by task killer app, the service also stops? am I right?

And, if I want to transfer this "life" value to display it as a label text in my Activity, what should I do? What is the difference if my activity is running and if my app is closed.

Please advice. If possible, can post a simple code to do this. I read through the tutorial and the Twiter example but not very understanding :( Thanks a lot.
 

thedesolatesoul

Expert
Licensed User
Longtime User
I need your suggestions. I'm making a game and I want to have "life" is added every hour (like Candy Crush, Farm Hero etc...) I'm confusing and not sure how to do this. I'm thinking of using service module and StartServiceAt. But, what happens if my app is killed by the phone or by task killer app, the service also stops? am I right?
No. Even if the service is stopped, StartServiceAt will start it again. This is the whole purpose of StartServiceAt.

Let the service update a process global variable and also write it to a file.

Your activity will read this file when it starts, otherwise if the life is incremented during the activity being active, updating the process global variable will update the variable too.
 
Upvote 0

ducphu

Active Member
Licensed User
Longtime User
Thanks for quick reply,

So in my main Activity_Create I should add StartService()
and in my Service Service_Start sub I should add StartServiceAt(). Am I correct?
So you mean even if my app and my service was killed, the service will auto start itself?
IF my activity is active, do I need to write code in my Sevice_Start sub to manually change the text displayed in my label?
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
You want to fire your service only when you increment the life?
In both places Activity_Create and Service_Start you can use StartServiceAt to schedule the next start, and update the life there.
In Service_Start you would want to write the lifes to a file, and also do a callsub on the activity, so it updates the life label if it is active.
 
Upvote 0

ducphu

Active Member
Licensed User
Longtime User
Let me explain a bit about my app. My idea is to start the service when app is launched. The service should stay alive and update "life" every hour.
So, to initialize the Service, I should use StartServiceAt() in my Activity_Create. Right?

My app has 2 activities, the "life" label is on activity 1. So to update the label, which should I do? CallSub or CallSubDelay (what if activity 2 is active and activity 1 is in background)

And also, how to determine whether my app is running before I do a CallSub?
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Let me explain a bit about my app. My idea is to start the service when app is launched. The service should stay alive and update "life" every hour.
So, to initialize the Service, I should use StartServiceAt() in my Activity_Create. Right?
Right.

My app has 2 activities, the "life" label is on activity 1. So to update the label, which should I do? CallSub or CallSubDelay (what if activity 2 is active and activity 1 is in background)
CallSub. Because callsubdelayed will also start activity 1, which is not what you want.

And also, how to determine whether my app is running before I do a CallSub?
It doesnt matter whether it is running or not.
 
Upvote 0

ducphu

Active Member
Licensed User
Longtime User
ok. So I create a sub

Sub Update_Life()
...
EndSub

and put it in my Activity1. In my Service_Start() I do CallSub(Activity1, "Update_Life") Right?
But why I read the wiki says:
CallSub (Component As Object, Sub As String) As String
Calls the given sub. CallSub can be used to call a sub which belongs to a different module.
However the sub will only be called if the other module is not paused. In that case an empty string will be returned.
You can use IsPaused to test whether a module is paused.
This means that one activity cannot call a sub of a different activity. As the other activity will be paused for sure.
CallSub allows an activity to call a service sub or a service to call an activity sub.
Note that it is not possible to call subs of code modules.
CallSub can also be used to call subs in the current module. Pass an empty string as the component in that case.

Which means if my activity is not running, CallSub wont work?
 
Upvote 0

ducphu

Active Member
Licensed User
Longtime User
Yes I will write it to a file. But again, does CallSub work if my activity not running or paused???
 
Upvote 0

ducphu

Active Member
Licensed User
Longtime User
Ok, then it's a problem. I should able to determine whether Activity1 is active, paused or not running at all, before I do a CallSub.
How can I check whether activity1 is active? I understand that can used IsPaused to test for pause. But how to test if activity is closed? Can I use IsInitialized ?
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
If you do a CallSub and the activity is not active then nothing will happen.
In my opinion you do not need to care whether or not it is paused.
But you can check it with IsPaused(Activity1).
There is no such thing as activity is closed. It is always considered as 'paused' if it is not in the foreground.
 
Upvote 0
Top