0

I am newbie at android programming. I would like to create a timer program that can set time by defining data independently.

For example, I can set 5 sec to start and 10 sec to stop a third party program which switch time like this until the Battery decrease to value that specify (assume 100 to 90 percent), then timer will stop at 90 percent.

Moreover, I would like to collect log data time of calling start and stop too. For instance, start 5 sec at 9.00 am and then stop at 9.06 to 9.15 am after that start 9.15 to 9.20 and also collect percent battery in each level.

When I turn the screen off would the timer still running or stopped because android go to sleep mode? Will the Log collect if I turn screen off? Should I code program as a service in order to run on background process?

Cheers Boy

Kevin Bedell
  • 13,254
  • 10
  • 78
  • 114

1 Answers1

2

there are many ways to perform background operations, but according to your description, it seems a classic case to use TimerTask class + Timer to schedule a background task:

http://developer.android.com/reference/java/util/TimerTask.html

also you can learn by this few examples how to work with it:

http://thedevelopersinfo.wordpress.com/2009/10/18/scheduling-a-timer-task-to-run-repeatedly/

http://android.okhelp.cz/timer-task-timertask-run-cancel-android-example/

How do you use a TimerTask to run a thread?

if you'd like the task to be interdependent (not attach to some activities live cycle), and run when you application is in background (no running activities of your app), then you should run the timer task from a Service. in that case - it doesn't matters if screen is on or off - the timer task will do the work according to the schedule.

Community
  • 1
  • 1
Tal Kanel
  • 10,475
  • 10
  • 60
  • 98
  • Hello Tal Kanel, after I tried to code like your example. It worked, but when I put in service and collect log. Log didn't write file except when I turn screen on. What should I do? – user1110973 Jun 18 '12 at 03:49
  • there is no suppose to be any connection between log messages to service, and screen on or off state, unless your code doing something which causing that to happen. if you don't see log messages when screen is off - it means only that nobody (you or the system) writing anything to log at this stage. – Tal Kanel Jun 18 '12 at 04:42
  • maybe if you'll post your code, I could tell if anything looks wrong – Tal Kanel Jun 18 '12 at 04:44
  • File is very large. So, I send this link https://dl.dropbox.com/u/11850339/StartStopTest.zip to you and after you download finish, I will delete it. The problem is that I try to collect log every start stop third party program by checking percent battery level. However, log does not collect when I turn screen off except I turn screen on. I try to avoid using wake lock, but It is not work because It has effect about consuming battery. – user1110973 Jun 18 '12 at 04:58
  • first of all - I see from the code you sent me that you didn't took my advice - and your time task been executed from the activity, and not from the service. another thing - you are doing handler.post(new Runnable()), and it means your code trying to be executed on the UI thread. if the screen is off - it can cause it not to be called!!! – Tal Kanel Jun 18 '12 at 05:09
  • Hello, I sent wrong link. This is correct link https://dl.dropbox.com/u/11850339/LogStartStop.zip – user1110973 Jun 18 '12 at 05:09
  • I looked on the new code, sorry, but I don't see nothing special suppose to cause the log not to be display. I advice you to check with another device. maybe your current firmware(ROM) version implement something which prevents log messages to be display when screen is off. it possibility. – Tal Kanel Jun 18 '12 at 05:14
  • anyway, I must go now, so good luck, and reward me for my help :) – Tal Kanel Jun 18 '12 at 05:15