Making app to use for certain duration of time - android

I'm newbie to android I'm creating a quiz app . I need to make changes in quiz app to used for certain duration of time for users after that it should say session expired.
I'm creating a quiz app . opening the app will open main activity which takes the user id and compares with the one in array. If it is true . it proceeds to next activity c1Activity in which question will be asked and user need to enter answer which is equated with stored value from array . if it is correct the user is proceeded to next question . similarly it go on until last question . my need is that the user should answer all questions within 3minutes if he lags the app should open a activity which shows times up

The most simple solution I can think for this is to implement a class that extends CountDownTimer.
First, you implement the class:
class MyTimer extends CountDownTimer{
// first param is the total amount of time it will countdown
// second param is to fire onTick() method, in case you need to fire an alert or something
public CountDown(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void onTick(long millisUntilFinished) {
//You can fire a Toast from here indicating how much time is left
}
public void onFinish() {
//You can start the new activity from here
}
}
Finally, you just have to create and start an object of this class:
//3 minutes = 3 * 60 * 1000
new MyTimer(180000, 1000).start();

Related

How do i increase the value of a textview with a handler? (or other solution)

first of all, i already tried this: Continuously increase integer value as the button is pressed
But i had 59 errors, yep, 59, and as i used to use Eclipse which told you CLEARLY what kind of error you had, how to fix it, and Android Studio looks that was made for people with experience... I can't even understand what the hell to do, to fix all errors (btw, when i try to fix something i break 10 more somehow).
So... Given a Button and a TextView how do i do to increase the textview (like a Clicker game for example) and make it stop pressing the same button again:
And how do i put the intervals between each "click"
TextView score = (TextView) findViewById(R.id.textView);
score.setText(Integer.toString(i));
Button click = (Button) findViewById(R.id.button2);
click.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
i++;
score.setText(Integer.toString(i));
}
});
By the way... I don't need the solution, i need to understand how exactly Thread or Handlers works, yes everybody will recommend me the Documentation, but i need to see a SIMPLE example explained part by part and i will understand way more than i already do by reading the documentation.
Given a Button and a TextView how do i do to increase the textview (like a Clicker game for example) and make it stop pressing the same button again: And how do i put the intervals between each "click"
Given your score and click widgets from your question:
Step #1: Add a Runnable field to your activity or fragment. Here, I'll call it incrementer.
Step #2: Define a static final int DELAY field in your activity or fragment, with the delay period you want ("intervals") in milliseconds.
Step #3: Have your Button use postDelayed() and removeCallbacks(), based on the state of incrementer:
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
if (incrementer==null) {
incrementer=new Runnable() {
#Override
public void run() {
i++;
score.setText(Integer.toString(i));
v.postDelayed(incrementer, DELAY);
}
};
incrementer.run();
}
else {
v.removeCallbacks(incrementer);
incrementer=null;
}
}
}
The incrementer field serves two roles. It tracks whether we are incrementing the TextView content or not, and it is the actual code that does the incrementing.
If incrementer is null, we are not presently incrementing the TextView. So, we assign incrementer a Runnable that can increment the TextView. The Runnable also calls postDelayed() to say "hey, run this Runnable again after DELAY milliseconds". We run() the Runnable ourselves the first time, to both populate the TextView at the outset and to trigger the postDelayed() call to schedule the next increment.
That will then continue to "loop" (run() calling postDelayed(), scheduling a future call to run()) until the user clicks the button again. Then, we see that incrementer is not null, so we must be incrementing the TextView and need to stop. removeCallbacks() unschedules the last postDelayed() call, stopping the "loop". We set incrementer to null mostly to prepare ourselves for the next button click.

Triggering An Event At Certain Dates Android

i am currently developing an auction app. each item will have a time frame before the auction ends on that item. i was wondering if anyone can give me an idea on how to listen to each item and check if the auction time has ended on that given item.
should i use a Service and build in it a trigger every second that checks each item in the database?
thank you in advance.
What I did for a previous app to solve a similar issue was to include the date/timestamp of the end date, and then implement a handler to implement a countdown until that date/time.
UpdateDisplay();
handler.removeCallbacks(updateTimeTask);
handler.postDelayed(updateTimeTask, 1000);
}
private Runnable updateTimeTask = new Runnable() {
public void run() {
UpdateDisplay();
handler.postDelayed(this, 1000);
Log.i(TAG, "End of updateTimeTask");
}
};
And inside UpdateDisplay() I kept the code for updating the UI, based on some logic subtracting the current time from the time remaining to show a second hand ticking down.

Checking date every time in android

I am developing an app. The basic concept of the app is that I have to keep few video playing continuously. The list of videos i.e playist is maintained by webservice. The playlist has dates on which date which playlist is to be played. My problem is how to check the date and load playist accordingly? The videos from proper playlist should keep on playing. So do I need to keep a service in background which checks the date each day or what do i need to do?
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
public void run()
{
webservicecall();
}
}, 2000); // 2sec
this is an eg. to set time interval u can use it logically as u want
Use a specific function which gets called by itself after your required time and check the time and date condition if matches do the task..
if u wish a delay/ recursive call function can be provided..

Random CountDownTimer Loop

I played a little around with CountDownTimer. However, for a special app I need the following functionality:
Start CountDownTimer with random runtime
Beep when finished
Wait 5 Seconds
Beep again
GOTO 1.
This procedure should be startet with a button and should also be canceled with a button. I found post here with a looped CountDownTimer, but this doesn't work with random runtime.
Okay, I'm not going to give you the code. That's your homework. I will however explain how the countDownTimer will work.
Once you've set up the CDT class create a new object.
private static myCDT newTime;//static makes sure there's only one instance of the variable in the entire program
newTime = new myCDT(randNum*1000, 1000);
Say randNum is 10, then the count down is for 10 seconds.
To generate the random number find out what you want the max limit to be. I'm assuming it to be 50. So the CDT will have an option from 0-50s.
int randNum=50*Math.random();
If you want to exclude 0 and want the minimum to be 1s
int randNum=1+49*Math.random();
Now also set a boolean variable for the button. The first time it's clicked let it be set to true. Use this variable as the condition in a while. Now, if the user clicks the button again, set the variable to false. Call the cancel() function for your CDT object newTime. Now call onFinish() function for your CDT. This will bring back flow of control to the while loop, the condition will now be false and so the loop stops. Make sure this loop is in the click listener function of the button. Or in any other function but be sure to call it from the click listener.

switching xml layout causes crash in Android

I need to write an android application like a book. I have approximately 100 images and i need to show them with back, forward and another buttons.
I tried to create an xml-layout for each image and made images to layout's background.
While running application, if i press buttons fast, program crashes during switching xml-layout.. If i decrease image sizes my problem decreases also. Unfortunately, i need another solution in order to solve it because i cannot use smaller image-size but i have crash problem still.
Have one layout, with an ImageView in it. Then keep changing the source image for the image view whenever you need to cycle to the next or previous image.
Part of the problem is that clicking a UI button returns immediately / queues clicks, even though the action associated with that click has not yet completed. For reasons beyond the scope of this response, its worth noting that simply deactivating the button while "doing work" is ineffective. There are a couple solutions to this kind of problem: One is to use a boolean flag that gets set only after the underlying "work" has completed. Then within the button action handler, you ignore button clicks that occur before the flag is reset:
/**
* Button presses are ignored unless idle.
*/
private void onMyButtonClicked() {
if(idle) {
doWork();
}
}
/**
* Does some work and then restores idle state when finished.
*/
private void doWork() {
idle = false;
// maybe you spin off a worker thread or something else.
// the important thing is that either in that thread's run() or maybe just in the body of
// this doWork() method, you:
idle = true;
}
Another generic option is to filter using time; ie. you set a limit where the max frequency of button presses is 1hz:
/**
* Determines whether or not a button press should be acted upon. Note that this method
* can be used within any interactive widget's onAction method, not just buttons. This kind of
* filtering is necessary due to the way that Android caches button clicks before processing them.
* See http://code.google.com/p/android/issues/detail?id=20073
* #param timestamp timestamp of the button press in question
* #return True if the timing of this button press falls within the specified threshold
*/
public static synchronized boolean validateButtonPress(long timestamp) {
long delta = timestamp - lastButtonPress;
lastButtonPress = timestamp;
return delta > BUTTON_PRESS_THRESHOLD_MS;
}
Then you'd do something like this:
private void onMyButtonClicked() {
if(validateButtonPress(System.currentTimeMillis())) {
doWork();
}
}
This last solution is admittedly non deterministic, but if you consider that users almost never intentionally click button more than 1-2 times a second on a mobile device, its not so bad.

Categories

Resources