Stop watch app inventor - android

I'm new user of App inventor. I need small stopwatch. Basic function:
- set time ex. 25 min (if time=0;alarm=true)
- start time
- reset time (if you want reset time you will see the comfir box (yes or no)
Can anybody show me what block I must use to create it ?. Sorry for my English.

Here is a link to a discussed about this in the App Inventor Coffee Shop.
https://groups.google.com/forum/?fromgroups#!topic/appinventor/qrhfCSv4US0

This block will need a label, and a timer.
You can choose to make it in seconds or milliseconds by changing the clock functions.

Here is an example. The user enters the duration into the minute and second textboxes. Then they press the button. The timer interval is set to the duration converted to milliseconds. Then the timer is enabled. After the duration is up, Clock1.timer fires. The notifier shows an alert that the timer finished and the timer turns off. To reset the timer, just set Clock1.timerEnabled to true.

Related

AlarmManager - When to setRepeating and how to get time remaining

I have an app which will give the user a bonus power up every 45 minutes. I managed to set a repeating alarm to do it but I have some questions:
1 - Where should I call setRepeating() method? - Having in mind that the alarm should be set automatically and NOT by an onClick event, for instance, I find it tricky to know where to set it. Now I am calling it on the onCreate method of my MainMenu activity. The problem is that everytime he user enters the app it is called. I thought about setting a SharedPreference variable to check if I already called it but it doesn't seem the right approach.
2 - How to the display the time left until next power up? - I would like to display to the user how much time is left until the next power up. The problem is that if the user closes the app and open it again I no longer have a reference to the alarm but it is still running. How can I do that?
Any help will be appreciated. Thanks in advance
I'd use shared preferences for both. When you set the alarm, write a shared preference that says when the next alarm is scheduled for. In onCreate, look at the alarm. If its in the future, no need to set it. If its in the past, set it again. The same answer solves problem 2- subtract the current time from the time in the shared preference, and that's the time left. When the alarm goes off, update the shared preference to the next time.

Android DatePicker.OnDateChangedListener constantly fires while spinning

Android's DatePicker.OnDateChangedListener constantly keeps firing while the user is spinning the calendar. I think this has changed in recent Android versions. Previously it only fired after the spinner stopped.
I would like to update content based on the new date, which involves web-access so this is too slow (and wasteful) to do on every event.
I prefer not to use a separate dialog to choose the date and update my content on the close-event of that dialog.
How do I know (trigger an event) when the datepicker has stopped spinning?
I can think of some mechanism with timers to check if the date is changed in, let’s say, the last second, but this looks pretty complex to me.
I solved it, by re-starting a CountDownTimer on every OnDateChangedListener-event. The timer is only 500 ms.
As long as the user is scrolling CountDownTimer will not finish. It is just constantly canceled and started on every OnDateChangedListener-event, until DatePicker is not scrolling anymore (not for 500 ms that is).

Take action when system clock changes minute?

My Android app has a TextView telling the user the data age ("13 minute ago"). I currently update this every second using a Runnable to catch when the minute changes with a notifyDataSetChanged() to my adapter. But this is causing garbage collection every second...
Is there another method to trigger a TextView update (or take general action) when the system clock changes minute without checking every second for such a change?
Since you already set up a Runnable and callback, use modulo to calculate when the next update should happen:
handler.postDelayed(runnable,
DateUtils.MINUTE_IN_MILLIS - System.currentTimeMillis % DateUtils.MINUTE_IN_MILLIS);
This will run the callback when the next minute occurs whether it is 59, 30, or 1 second(s) away. As a note, if the Runnable only updates one TextView don;t forget to switch to DateUtils.HOUR_IN_MILLIS when the event is an hour old, no sense updating "1 hour ago" every minute.
You may find methods like DateUtils.getRelativeTimeSpanString() useful for creating the "x minutes ago" strings.
While Sam's answer is perfectly good, I'm just adding this because it fits the question so perfectly. There is actually a broadcast sent by the system every minute when the clock changes. It is Intent.ACTION_TIME_TICK and you can only receive it using a BroadCastReceiver that was registered manually in your code, not through the manifest. See the documentation here.
User the Timer object with fixed period.
The methods take a TimerTask object.
See the Timer documentation from google
REMEMEBER to cancel the timer when stopping the activity
There is prettier solution for this besides Sam's great answer.
Make use of http://developer.android.com/reference/android/text/format/DateUtils.html to find the time span and register for android.intent.action.TIME_TICK where you'll be updating the time in the listview.
Here is another same question and answer

Android: start listener after timing

i've an activity with a ToggleButton that must start (if checked on) or stop (if checked off) a listener.
The problem is that i want to start the listener after a specific time (10 sec for example) but keeping the ToggleButton active and if the user click on it before the timing ending, abort the timing and listener activation.
I'm in confusion with the correct way to do that... do you have any idea?
thanks in advance
Use a Timer and TimerTask.
Put the timer on 10 seconds delay.
If the user clicks the button again on these 10 seconds, cancel the Timer.
If not, start the listener.
Note that in order to change UI elements through the TimerTask thread, you will have to use an handler.

robotium testing wait time between each clicks

in robotium testing, is it possible to set a wait time between clicks? For example, I have 2 buttons(A & B). I want robotium to click on button A and then 20 seconds later click on button B.
u can use this code its working fine
solo.clickOnButton(0);
solo.sleep(20000); // 20 seconds
solo.clickOnButton(0);
If the purpose is to wait for a fixed amount of time then use solo.sleep(20000). If you want to wait for a condition before moving on you can use the waitFor methods.

Categories

Resources