Best way to do a background timer - android

Im trying to make a simple game. The game is going great only I want to use a background timer. Like in citybuild games, a task takes x hours to complete and after that there should be a notification. What is the best way to do this? Do I need a background service, an alarmmanager, both? and is it smart to also keep the start timer of the task in an SQLLite db so if the timer stops he can still see it after it starts in the db?
Basicly what I'm asking is, what is best practive and do you guys have some pointers or better yet, examples/tutorials for me?

I recommend you to use the AlarmManager, it's native, you don't have to implement some background service, it doesn't consume battery, and it's easy to use.
Please refer to this tutorial.
Also, you can save the start time in SharedPreferences (or DB, as you prefer), and when the user re-starts the app, check if the job isn't already completed. This is an extra-step to make sure everything works as expected even if AlarmManager goes wrong

Related

In Android it is better to use bounded service for communicating with a fragment or a service with a eventbus system (MessageBus) is enough?

Im building an Android App in Xamarin that plays music.
I have a service that plays the music and a fragment that display the playlist, some times i want a small feedback from the service and so far i was using MessageBus for the communication.
For now the only time that i want a feedback is when the user has select to loop the whole list and when the current track is finished i need to inform the fragment which is the next track in order to highlight it. maybe in the future i will need for feedback.
Is this a bad idea? i should better change the service with a boundService? I have select this method because it is much simpler and the feedback i want is very limited but now i have second thoughts.
Thanks
As long as you only need to communicate within your own application, there's nothing wrong with using a message bus.
I do this quite a bit, and it works well, and is much less complicated than other methods.
Just make sure that you know the threading model that your event bus is using, and be careful to do non-ui things on a background thread, and ui updates on the main ui thread.

AsyncTask randomly stops

I have a pull based app that has to pull from the Server every 15 seconds. For that I used a normal AsyncTask, my code basically looks like this:
protected Void doInBackground(Context... arg0) {
while (true && this.pull_on ) {
pull_data();
TimeUnit.SECONDS.sleep(15)
};
Now I have the massive Problem that, the app just stops pulling out of the blue. No exeptions, the Status of the AsyncTask is 'running' the 'pull_on' variable is set to 'true' but it just stops pulling.
I can not reproduce this, it just happens sometimes, I can work perfectly for a long time and then it just stops.
Does anybody have an idea how this can happen?
Should I refactor and implement this with a timer?
Also what benefit would using a Service have over what I do know? Battery-power is not an issue.
Thanks for your help.
P.S. I should add that sometimes I start Activitys form the AsyncTask. Like this 'ApplicationManager.getCurrentActivity().startActivity( myIntent )' but I does not seam to be a Problem.
P.P.S.
Timer seams to be to limited for what I want to use, I do want to do something every 15 seconds but in some special case I want to stop the the pinging and then restart it again. The amount of hacking for that seams to be more work then just doing it with a AsyncTask.
That is a terrible design. You do not want to have an infinite loop on the background every 15 seconds. That will drain battery when the app is pushed to the background, as one of the main problems.
One proper way to do it is to have an Alarm and react to that. A timer is not a bad option either.
I suggest you look into moving this to a service, which is what it looks like
A Service is an application component that can perform long-running operations in the background and does not provide a user interface
It talks about long-running operations, but recurrent operations also make sense to be moved to a service.
Official documentation on Services is here: http://developer.android.com/guide/components/services.html

Android - What could be the ways in which once app starts it wait for tasks to be performed and exits after that?

I was wondering how about the different ways in which an app can be opened, some tasks performed on the main thread and then be finished. And which way would be the best for this purpose.
I thought using an asynctask and putting the thread to sleep for sometime should be helpful and in postexecute perform the task that i want. But I am pretty sure this is a very expensive solution.
For example, If I want the app to click a photo on its own when the app is opened. The app needs to be opened and then wait so that the camera resources can be loaded properly. Once this is done the photo is clicked and once the photo is saved the app will finish.
Please provide some links if you can so I can read up and understand these concept better.
What are the better ways to go about solving this problem than using thread.sleep()..
Please share any good methods..
Thank you.
if I understand what you need (I'm not sure) you can use broadcast to cominicate between tasks

Service accessing a database

Ok guys, I did ask before on how to create a way to run a code always in my app. It's similar to how say iMobeter or something like that check your hp, if it's less than max it'll add to it every 5 mins.
Some people told me to use service which sounded good. Problem is, I need a service that is always running in the background. Plus, I could not create a service that can access the database. I cant use the cursor there as in it does not support the method.
I would appreciate pointing me to a good example or a better way to do it.
Problem is, I need a service that is always running in the background.
That is not possible. Users can stop your service whenever they want, and Android will stop your service if it lives too long.
Plus, I could not create a service that can access the database. I cant use the cursor there as in it does not support the method.
Yes, it does. Use SQLiteOpenHelper, the same way you would with an activity, content provider, etc.

Advice on structuring a service, activity, thread

can someone please help me?
I would like to write a program which uses a service to periodically update a text view on an activity.
I do this by having ActivityA with a 2 buttons to start/stop my service. In the service I run a timer which triggers every second. From here I need to have this launch and update a text view on ActivityB which at present is just a counter value.
I'm sure there are likely better ways to do this, such as using only one activity, maybe using a thread but the main design consideration is to have the service running even if my activity is destoyed (the counter value would instead go trigger some alarm or file write instead of a text view update).
Sorry for rambling. I find the android developer resources offer too many solutions!
Thanks
Ben
In the service I run a timer which triggers every second.
Why? Most Android devices run on batteries. Batteries are never big enough. What value are you giving the user to justify your expenditure of CPU and RAM (and, hence, battery life)?
From here I need to have this launch and update a text view on ActivityB which at present is just a counter value.
Where is "here"?
I'm sure there are likely better ways to do this, such as using only one activity
I would think so.
maybe using a thread
Probably not.
but the main design consideration is to have the service running even if my activity is destoyed
This is significantly more complicated than you are perhaps thinking.
(the counter value would instead go trigger some alarm or file write instead of a text view update).
If your goal is to do something at a particular time, use AlarmManager.
I suspect that there is a better approach for whatever it is that you are trying to do than the path you are presently headed down. Unfortunately, since I do not know what it is that you are trying to do, I have limited ability to provide more specific advice.
I think what you want to do is at best done with an simple AsyncTask. If you use the onProgressUpdate method you can increase the value in the textview at every time you reach a certain point during your background work. It is also able to cancel the background work etc. There is no need for the full Service, Thread work.

Categories

Resources