I have run into a problem. I have an animation that is going in my first activity, and a countdown timer in my second activity. When I set the countdown timer the value is passed back into my first activity which begins a new countdown activity, but the animation stops. How can I pass the value of the countdown timer to my MainActivity while still Maintaining the animation in my main activity.
Here is my code for the intent in my SecondActivity that is being passed to my MainActivity. Im not sure what code to show because I am not sure what the cause of the problem is.
buttonStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//put your logic here to set the time with hourPicked and minPicked variables
timer = new CounterClass((hours * 60 * 1000) + (minutes * 1000), 1000);
String hms = String.format(("%02d:%02d"), hours, minutes);
textViewTime.setText(hms);
Intent intent = new Intent(SleepTimer.this, MainActivity.class);
String TimeValue = (hms);
intent.putExtra("TimeValue", TimeValue);
startActivity(intent);
timer.start();
}
});
You have to declare String hms globally and onclick of button finish the SleeoTimer activity.and access global variable hms at MainActivity in SleepTimer Activity create field as public static String hms;
and in MainActivity use it as SleepTimer.hms
buttonStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//put your logic here to set the time with hourPicked and minPicked variables
timer = new CounterClass((hours * 60 * 1000) + (minutes * 1000), 1000);
hms = String.format(("%02d:%02d"), hours, minutes);
textViewTime.setText(hms);
SleepTimer.this.finish();
}
});
You have to add your animation code on Activity Oncreate,OnPause and also in OnResume as per your requirement.
Related
I have a DialogFragment which has a listView as container. I've set up OnItemClickListener on the listView.
How can I get the value when the user touchs an item and pass it to another activity then store that value into a variable? I need to set a count down timer depending on which item will be selected. Actually can only display simple toast message with the item position.
As a note, the activity to which the value will be passed is not the fragment activity.
I was thinking about Bundle but have not having much knowledge on programming is a pain even after reading documentation on Google site.
on the MainActivy, here is how I set the timer:
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
String elapsedTime = String.valueOf(millisUntilFinished / 1000);
timer.setText(elapsedTime);
}
public void onFinish() {
timer.setText(R.string.text);
}
}.start();
}
Exemple:
If user touches item 1 then the timer gets value 15 minutes and so on...
Please guide me, thank you.
So, you want to pass data from Fragment to Activity. Here is how you do it.
Passing the data from Fragment to Activity:
final Intent intent = new Intent(getActivity(), ActivityName.class);
intent.putExtra("counter-value", counterValue);
getActivity().startActivity(intent);
Reading the value in the Activity:
final Bundle extras = getIntent().getExtras();
if(extras != null) {
final int counterValue = extras.getInt("counter-value", -1);
showCountDown(counterValue);
}
Now, Pass the value to the countdowntimer.
private void showCountDown(final int counterValue) {
new CountDownTimer(counterValue, 1000) {
public void onTick(long millisUntilFinished) {
String elapsedTime = String.valueOf(millisUntilFinished / 1000);
timer.setText(elapsedTime);
}
public void onFinish() {
timer.setText(R.string.text);
}
}.start();
}
I have an edit text for entering a time in minutes, a button to start a countdown timer using the minutes entered in the edit text which updates a text view, and a button which I want to cancel the countdown timer before it finishes.
The problems I'm having are...the variable the countdown timer needs (the milliseconds) can't get assigned until I click the start timer button because it's coming from an edit text. So I created the countdown timer inside of the onClick for the start timer button. However...I want another button to cancel the timer...and it doesn't have access to the countdown timer. So that didn't solve anything and I'm confused at how to do this. Any advice would help.
Edit: I just realized I could move the stop timer button with the listener to the start timer button onClick also...under the countdown timer. I'm just confused about how the countdown timer could find the milliseconds variable if I have countdown timer outside of the start timer onClick. I guess moving the stop timer under countdown timer works...but still feels like I'm missing something.
Edit2: I tried moving countDownTimer into onCreate and making milliseconds global...but I have to make countDownTimer final in order for the buttons to be able to use it, and countDownTimer just skips to onFinish.
mTimerTextView = (TextView)findViewById(R.id.timer_text_view);
mTimerEditText = (EditText)findViewById(R.id.timer_edit_text);
mStartTimerButton = (Button)findViewById(R.id.btn_start_timer);
mStartTimerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Getting time in minutes from edit text
int timeEntered = Integer.parseInt(mTimerEditText.getText().toString());
long milliseconds = timeEntered * 60000;
CountDownTimer countDownTimer = new CountDownTimer(milliseconds, 1000) {
#Override
public void onTick(long millisUntilFinished) {
int seconds = (int) (millisUntilFinished / 1000) % 60;
int minutes = (int) ((millisUntilFinished / (1000*60)) % 60);
int hours = (int) ((millisUntilFinished / (1000*60*60)) % 24);
mTimerTextView.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds));
}
#Override
public void onFinish() {
new AlertDialog.Builder(DailyGoalActivity.this)
.setTitle("Time's Up")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).show();
}
};
countDownTimer.start();
}
});
mStopTimerButton = (Button)findViewById(R.id.btn_stop_timer);
mStopTimerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// countDownTimer.cancel(); <- How could I do this?
}
});
there are two possible reason for your problem first one is that you need to create countdown timer in onCreate and second one is that you are stopping null countdown timer.
solution-1 you can use a boolean variable to check that timer has start or not and then start timer accordingly on button click.Apart from this initialize timer in on create and start in on click follow this code please follow this link
solution-2 to stop the timer use this code
if(countDownTimer != null) {
countDownTimer .cancel();
countDownTimer = null;
}
Isn't the simple solution to declare CountDownTimer variable globally?
Move this code to onCreate
CountDownTimer countDownTimer = new CountDownTimer(milliseconds, 1000) {
#Override
public void onTick(long millisUntilFinished) {
int seconds = (int) (millisUntilFinished / 1000) % 60;
int minutes = (int) ((millisUntilFinished / (1000*60)) % 60);
int hours = (int) ((millisUntilFinished / (1000*60*60)) % 24);
mTimerTextView.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds));
}
}
Then start timer in Start button's onClick and cancel in Stop button's onClick.
Hope it helps.
Im working on a Quiz App. in which I have a different types of quizzes. Quiz with Time and Quiz without Time. Quiz without time is simple and there is no certain or specific time given to attempt the quiz. But the Quiz with Time is time restricted Quiz.
What I Want:
When an individual try to attempt a time restricted quiz. The certain amount of time will be show on an activity for instance remaining time (30 min left). and after the 30th min the activity automatically stop. I have tried timer class but that is used for delay.
how can I stop a activity after a certain time?
Thanks in Advance
public void countDown() {
new CountDownTimer(60000*30, 1000) {
public void onTick(long millisUntilFinished) {
int seconds = (int) (millisUntilFinished / 1000) % 60 ;
int minutes = (int) ((millisUntilFinished / (1000*60)) % 60);
int hours = (int) ((millisUntilFinished / (1000*60*60)) % 24);
String my_new_str =((((((((( (pad(hours)+":"+pad(minutes)+":"+pad(seconds)));
timer.setText( my_new_str);
float pvalue = ( float)(millisUntilFinished*100)/(60000*30);
myprogressbar.setProgress((int)(Math.round(pvalue)));
}
public void onFinish() {
timer.setText("Completed");
goToResult();
this,finish();
}
}.start();
}
you can do this using Handlers
try some thing like this...
Handler handler = new Handler();
Runnable x=new Runnable() {
#Override
public void run() {
finish();
}
};
handler.postDelayed(x, 6000);
Put your question in Thread...
Thread t=new Thread()
{
public void run()
{
try{
sleep(30000);
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
Intent intent = new Intent(Splash.this, Login.class);
startActivity(intent);
finish();
}
}
};
t.start();
Try to use AsyncTask or Handler in your Activity. i think you set some certain time limits for the
Quiz Question so please make sure to use any one of this above i mentioned.
Handler handler = new Handler();
Runnable x=new Runnable() {
#Override
public void run() {
Intent intent = new Intent(CurrentActivity.this, TargetActivity.class);
startActivity(intent);
finish();// activity will kill. or use
System.Exit();
}
};
handler.postDelayed(x, yourtimelimit(in timemills or millisecond));
Even you doesn't work yet means try to use onComplete listener and the put it finish in that method or
use onDestroy method inside to finish().
thank you
I'm making an application contains about 20 activities and I want to start a count up timer when the activity 1 starts and finish counting on the last activity. I've found a way to make a subclass and call the timer in 1 activity But I didn't know how to pass the value of the timer from activity 1 to activity 2 and from 2 to 3 . this is my code
subclass
package com.mytimer;
import java.lang.ref.WeakReference;
import java.util.TimerTask;
import android.os.Handler;
import android.widget.TextView;
public class IncrementTask extends TimerTask {
WeakReference<TextView> mRef;
int counter = 0;
Handler handler = new Handler();
public IncrementTask(TextView text) {
mRef = new WeakReference<TextView>(text);
}
public void run() {
handler.post(new Runnable() {
public void run() {
mRef.get().setText("counter " + counter);
counter++;
}
});
}
}
in my activity 1
TextView mTextView = (TextView)findViewById(R.id.text);
Timer timer = new Timer();
IncrementTask task = new IncrementTask(mTextView);
timer.scheduleAtFixedRate(task, 0, 1000);
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Intent i = new Intent (MainActivity.this, Page2.class);
startActivity(i);
}
});
I want to know how to pass the value of the timer to the next activity not start the timer from 0
any help please
In my opinion use Fragments instead of Activity because if you use fragments it is able to show two fragment on screen which contains timer and your remaining content.
If you use activity it is not possible to show accurate timer values while you move from once screen to other screen.
How do you start the subsequent activities? I suppose with startActivity() or startActivivtForResult() , if this is the case you cold pass the time in the intent.
But it is not a good solution because in the process you would "lose time" because the activity staring is not exact to the second.
Why not create a timer activity that starts at the end of the process and finishes at the end?
I have the service working now. I'm just having trouble getting information back from the service to the main activity.
I have added a function to try and retrieve the data from the service which is launched after the button click in the main activity launches the service:
startService(passvars);
//Init. variable for getDisplayInf() function to update the display with counter strings
Running = 1;
getDisplayInf();
And the getDisplayInf() ex:
public void getDisplayInf() {
//Intent stIntent = new Intent(MainActivity.this,MainActivity.class);
//Intent passvars = new Intent(MainActivity.this,Service.class);
Bundle getvars = getIntent().getExtras();
String Remaining;
String CountDown;
do {
Remaining = getvars.getString("Remaining");
CountDown = getvars.getString("CountDown");
mRemaining.setText(Remaining);
mCountDown.setText(CountDown);
Running = getvars.getInt("Running");
}
while (Running == 1);
};
The timer will set the Running variable to 0 on finish inside of the service.
As soon as I click the button with this new function in place it crashes the app, so I'm assuming it has to do with the Bundle/intent I'm using in the getDisplayInf() code.
In the service I'm doing this on the counter's onTick:
#Override
public void onTick(long millisUntilFinished) {
Running = 1;
Remaining = "Time Remaining: ";
CountDown = formatTime(millisUntilFinished);
ticker();
}
ticker's code:
public void ticker () {
Intent passvars = new Intent(Service.this,MainActivity.class);
//this is for the activity to keep looping to grab the countdown values while running this timer
//now send it to the activity
passvars.putExtra("Running", Running);
//String Values to be passed to the activity
//now send it to the activity
passvars.putExtra("mRemaining", Remaining);
//now send it to the activity
passvars.putExtra("mCountDown", CountDown);
};
Am I going about this in the right way? Is there an easier way? Some help would be greatly appreciated!!!