I have a runnable timer that update a textview every second, when the activity is onStop (or called into the background) the timer continues to run. The issue i am having is that when i re-launch the activity it starts the same timer again, so the numbers are going up twice as fast as they should. I have it coded so that it will kill both timers before it restarts them but i believe that when the activity is started again the timers are not being killed. Here is an example of my code :
t.cancel();
cd.cancel();
t = new Timer();
t.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
That is only a small part but it should kill the timer (t.cancel();) then start a new one, this only happens when the activity is stopped and then restarted. please help this issue is driving me absolutely insane.
=========================================================
For the brave souls willing to read alot, here is my entire activity that i am having the issue with:
public class PayTracker extends Activity {
private static double Reserve;
private static int Reserve1;
public static double money;
public static double counter;
private static int go;
private static int countdown;
public static int convert;
public static double HW;
public static double OTW;
public static double HPD;
public static double DPPS;
public Timer t = new Timer();
public Timer cd = new Timer();
public static String mcountdown = "Time till overtime";
public static String mmoney = "total cash";
public static String mcounter = "ticks";
public static String mReserve = "building total";
public static String mReserve1 = "building total 2";
public static String mHW;
public static String mOTW;
public static String mHPD;
public static String mDPPS;
public static String mgo;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pay_tracker);
getActionBar().setDisplayHomeAsUpEnabled(true);
// Receive messages from options page
double pHW, pOTW, pHPD;
Intent intent = getIntent();
pHW = intent.getDoubleExtra(Options.MESSAGE_HW, 0);
pOTW = intent.getDoubleExtra(Options.MESSAGE_OTW, 0);
pHPD = intent.getDoubleExtra(Options.MESSAGE_HPD, 0);
if(pHW != 0){
HW = pHW;
OTW = pOTW;
HPD = pHPD;
}
// Color buttons
Button buttonc = (Button) findViewById(R.id.clockin);
buttonc.getBackground().setColorFilter(0xFF00FF00, PorterDuff.Mode.MULTIPLY);
Button buttond = (Button) findViewById(R.id.clockout);
buttond.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);
// go = 0;
// Calculate pay per second
final double PPS = (HW/3600);
DPPS = (PPS/50);
final double OTPPS = (OTW/3600);
final double DOTPPS = (OTPPS/50);
final double HPDPS = (HPD*3600);
final double DHPDPS = (HPDPS*50);
// Display
final TextView t1 = (TextView) findViewById(R.id.yourpay);
t1.setTextColor(Color.parseColor("#008000"));
final TextView t2 = (TextView) this.findViewById(R.id.payper);
final String result2 = String.format("%.8f", OTPPS);
final String result = String.format("%.8f", PPS);
// if(go != 1){
// go = 1;
// if(go == 1){
t.cancel();
cd.cancel();
// go = 0;
// }
// if(go == 0){
// go = 1;
t = new Timer();
t.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
if(DHPDPS==0){
money = (DPPS+Reserve);
Reserve = (money);
String end = String.format("%1f", money);
t1.setText("$" + end);
}else if(counter > DHPDPS && DOTPPS != 0 && DHPDPS != 0){
money = (DOTPPS+Reserve);
Reserve = (money);
String end = String.format("%1f", money);
t1.setText("$" + end);
} else{
money = (DPPS+Reserve);
Reserve = (money);
String end = String.format("%1f", money);
t1.setText("$" + end);
}
counter++;
//if(counter == 3000)
// t.cancel();
// Display pay per second
if(counter <= DHPDPS || DHPDPS == 0){
t2.setText("Your pay per second is: $"+result);
}else{
t2.setText("Your pay per second is: $"+result2);
}
}
});
}
}, 20, 20);
// Make countdown to overtime display
final Intent intent1 = new Intent(this, PayTracker.class);
// Create the notification
final Notification notification = new Notification(R.drawable.ic_launcher, "Click here to check your pay!", System.currentTimeMillis());
// Create an Intent for the notification to launch
// Create a PendingIntent for the associated Intent
final PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent1, 0);
cd = new Timer();
final TextView count = (TextView) findViewById(R.id.countdown);
convert = (int)HPDPS;
cd.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run(){
countdown = (convert - Reserve1);
int hours = (countdown/3600);
if(OTPPS != 0 && HPDPS != 0){
count.setText("Seconds Remaining to Overtime: " + countdown + "\nAbout " + hours + " Hours");
Reserve1++;
}
// Set the notification's details
final String end = String.format("%.6f", money);
notification.setLatestEventInfo(getApplicationContext(), "Your Current Pay:", "$"+end, pendingIntent);
// Submit the notification to the system
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(0, notification);
}
});
}
}, 1000, 1000);
// }
// }
final Button b = (Button) findViewById(R.id.clockout);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(go == 1)
go = 0;
if (t != null){
t.cancel();
cd.cancel();
}
}
});
}
public void onRestoreInstanceState(Bundle savedInstanceState) {
// Always call the superclass so it can restore the view hierarchy
super.onRestoreInstanceState(savedInstanceState);
// Restore value of members from saved state
countdown = savedInstanceState.getInt(mcountdown);
Reserve = savedInstanceState.getInt(mReserve);
money = savedInstanceState.getInt(mmoney);
counter = savedInstanceState.getInt(mcounter);
Reserve1 = savedInstanceState.getInt(mReserve1);
HW = savedInstanceState.getInt(mHW);
OTW = savedInstanceState.getInt(mOTW);
HPD = savedInstanceState.getInt(mHPD);
DPPS = savedInstanceState.getInt(mDPPS);
go = savedInstanceState.getInt(mgo);
}
#Override
public void onStart(){
super.onStart();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_pay_tracker, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
public void sendMessage(View view) {
// Calculate pay per second
final double PPS = (HW/3600);
DPPS = (PPS/50);
final double OTPPS = (OTW/3600);
final double DOTPPS = (OTPPS/50);
final double HPDPS = (HPD*3600);
final double DHPDPS = (HPDPS*50);
// Display
final TextView t1 = (TextView) findViewById(R.id.yourpay);
t1.setTextColor(Color.parseColor("#008000"));
final TextView t2 = (TextView) this.findViewById(R.id.payper);
final String result2 = String.format("%.8f", OTPPS);
final String result = String.format("%.8f", PPS);
//if(go != 1){
// go = 1;
t.cancel();
cd.cancel();
t = new Timer();
t.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
if(DHPDPS==0){
money = (DPPS+Reserve);
Reserve = (money);
String end = String.format("%1f", money);
t1.setText("$" + end);
}else if(counter > DHPDPS && DOTPPS != 0 && DHPDPS != 0){
money = (DOTPPS+Reserve);
Reserve = (money);
String end = String.format("%1f", money);
t1.setText("$" + end);
} else{
money = (DPPS+Reserve);
Reserve = (money);
String end = String.format("%1f", money);
t1.setText("$" + end);
}
counter++;
if(counter == 3000)
t.cancel();
// Display pay per second
if(counter <= DHPDPS || DHPDPS == 0){
t2.setText("Your pay per second is: $"+result);
}else{
t2.setText("Your pay per second is: $"+result2);
}
}
});
}
}, 20, 20);
// Make countdown to overtime display
final Intent intent1 = new Intent(this, PayTracker.class);
// Create the notification
final Notification notification = new Notification(R.drawable.ic_launcher, "Click here to check your pay!", System.currentTimeMillis());
// Create an Intent for the notification to launch
// Create a PendingIntent for the associated Intent
final PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent1, 0);
cd = new Timer();
final TextView count = (TextView) findViewById(R.id.countdown);
convert = (int)HPDPS;
cd.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run(){
countdown = (convert - Reserve1);
int hours = (countdown/3600);
if(OTPPS != 0 && HPDPS != 0){
count.setText("Seconds Remaining to Overtime: " + countdown + "\nAbout " + hours + " Hours");
Reserve1++;
}
// Set the notification's details
final String end = String.format("%.6f", money);
notification.setLatestEventInfo(getApplicationContext(), "Your Current Pay:", "$"+end, pendingIntent);
// Submit the notification to the system
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(0, notification);
}
});
}
}, 1000, 1000);
//}
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putInt(mcountdown, countdown);
savedInstanceState.putDouble(mReserve, Reserve);
savedInstanceState.putDouble(mmoney, money);
savedInstanceState.putDouble(mcounter, counter);
savedInstanceState.putDouble(mReserve1, Reserve1);
savedInstanceState.putDouble(mHW, HW);
savedInstanceState.putDouble(mOTW, OTW);
savedInstanceState.putDouble(mHPD, HPD);
savedInstanceState.putDouble(mDPPS, DPPS);
savedInstanceState.putInt(mgo, go);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
#Override
public void onDestroy() {
super.onDestroy();
if(t != null)
t.cancel();
if(cd != null)
cd.cancel();
}
}
This is one way to get around that.
static Timer mTimer = null;
onCreate() {
if (mTimer == null)
mTimer = new Timer();
} else {
// You shouldn't have to do nothing because your timer should be running
}
}
Note that there are several issues in general here. The static is basically saying just to create one instance of that object. As a side effect it also tries to reclaim the same memory address. Either way, once your app is in the background it can be cleaned up by the system at any time so your not guaranteed to get your Timer back. There are a bunch of other ways to get around that, but that is out of scope for this question.
Related
I'm creating a timer app that utilizes a thread. When I exit the app using the home button the timer is running fine. Usually, when the time is up a dialog is launched that asks the user for some input. This works completely fine if the app is in its onResume() state, however when the app is in its onStop() state the dialog will not launch and an error is thrown.
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
How can I make it so that when the time is up and the app is NOT in the foreground the dialog is still launched. My initial thought was to save the time remaining in the bundle, but the time remaining is changing for every single tick. Then I thought about storing a boolean in the bundle mTimeRunning. However, when the time is up this value must also change. So now I'm drawing a blank. What can I possibly do so that the dialog is launched when the app is not in the foreground?
TimerActivity.java
public class TimerActivity extends AppCompatActivity implements TimeDialogFragment.sendMinutes,
TimeFinishDialogFragment.sendResponse, BreakFinishDialogFragment.userResponse {
// Variable to log activity state
private static final String TAG = "TimerActivity";
private static final boolean DEBUG = true;
// ^^ Variable used to log acitivty state
private Handler mHandler;
private Runnable mRunnable;
//private static final long START_TIME_MILLISECONDS = 600000;
// Below start time is for development purposes only
private static long mStartTime = 10000;
private long mTimeRemaining = mStartTime;
private boolean mTimeRunning;
private boolean mBreakTime = false;
private ProgressBar mTimeBar;
private TextView mTime;
private Button mStartPause;
private Button mSetTime;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer);
if(DEBUG) Log.d(TAG, "+ onCreate() +");
mHandler = new Handler();
mTimeBar = findViewById(R.id.time_bar);
mTime = findViewById(R.id.text_view_time);
mStartPause = findViewById(R.id.button_start_pause);
mSetTime = findViewById(R.id.button_set_time);
updateCountDownText();
mTimeBar.setMax((int) mTimeRemaining);
mSetTime.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//FragmentManager manager = getSupportFragmentManager();
DialogFragment setTime = new TimeDialogFragment();
setTime.show(getFragmentManager(),"SET_TIME_DIALOG");
}
});
mStartPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(mTimeRunning){
//pauseTimer();
mTimeRunning = false;
mStartPause.setText("Start");
mHandler.removeCallbacks(mRunnable);
}else{
//startTimer();
timer();
}
}
});
}
// Using a handler + anon runnable
private void timer(){
mRunnable = new Runnable() {
#Override
public void run() {
mTimeRunning = true;
mStartPause.setText("Pause");
mTimeRemaining = mTimeRemaining - 1000;
updateCountDownText();
mTimeBar.incrementProgressBy(1000);
if(mTimeRemaining > 0) {
mHandler.postDelayed(this, 1000);
}else{
// if breaktime is false
if(!mBreakTime) {
DialogFragment dialog = new TimeFinishDialogFragment();
dialog.show(getFragmentManager(),"TIME_FINISH_DIALOG");
mTimeRunning = false;
}else{
// launch break time up dialog.
mBreakTime = false;
DialogFragment dialog = new BreakFinishDialogFragment();
dialog.show(getFragmentManager(), "BREAK_FINSIH_DIALOG");
}
}
}
};
mHandler.postDelayed(mRunnable,1000);
}
public void updateCountDownText(){
int min = (int) (mTimeRemaining / 1000) / 60;
int sec = (int) (mTimeRemaining / 1000) % 60;
String formattedString = String.format(Locale.getDefault(), "%02d:%02d", min, sec);
mTime.setText(formattedString);
}
public void setCountDownText(long time){
int min = (int) (time / 1000) / 60;
int sec = (int) (time / 1000) % 60;
String formattedString = String.format(Locale.getDefault(), "%02d:%02d", min, sec);
mTime.setText(formattedString);
}
#Override
public void userTime(int minutes) {
TimerActivity.mStartTime = (minutes * 60) * 1000;
mTimeRemaining = TimerActivity.mStartTime;
mTimeBar.setMax((int) mTimeRemaining);
setCountDownText(mTimeRemaining);
}
#Override
public void sendResponse(int val) {
if(val == -1){
mTimeRemaining = TimerActivity.mStartTime;
mTimeBar.setMax((int) mTimeRemaining);
updateCountDownText();
mTimeBar.setProgress(0);
mStartPause.setText("Start");
}else if(val == 1) {
mBreakTime = true;
mTimeRemaining = 15000;
mTimeBar.setMax((int) mTimeRemaining);
setCountDownText(mTimeRemaining);
mTimeBar.setProgress(0);
mStartPause.setVisibility(View.INVISIBLE);
timer();
}else {
mTimeRemaining = TimerActivity.mStartTime;
mTimeBar.setMax((int) mTimeRemaining);
updateCountDownText();
mTimeBar.setProgress(0);
timer();
}
}
#Override
public void userResponse(int val) {
if(val < 0) {
// user clicked cance
mTimeRemaining = TimerActivity.mStartTime;
mTimeBar.setMax((int) mTimeRemaining);
updateCountDownText();
mTimeBar.setProgress(0);
mStartPause.setText("Start");
}else {
mTimeRemaining = TimerActivity.mStartTime;
mTimeBar.setMax((int) mTimeRemaining);
updateCountDownText();
mTimeBar.setProgress(0);
timer();
}
mStartPause.setVisibility(View.VISIBLE);
}
TimeFinishedDialog.java
public class TimeFinishDialogFragment extends DialogFragment implements View.OnClickListener{
private Button mCancel;
private Button mSkip;
private Button mStartBreak;
private sendResponse mResponse;
interface sendResponse{
void sendResponse(int val);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//return super.onCreateDialog(savedInstanceState);
View view = LayoutInflater.from(getActivity()).inflate(R.layout.time_finish_dialog_fragment, null, false);
mCancel = view.findViewById(R.id.button_cancel);
mSkip = view.findViewById(R.id.button_skip);
mStartBreak = view.findViewById(R.id.button_start_break);
mCancel.setOnClickListener(this);
mSkip.setOnClickListener(this);
mStartBreak.setOnClickListener(this);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(view)
.setTitle("Start Break?");
AlertDialog dialog = builder.create();
dialog.setContentView(view);
return dialog;
}
#Override
public void onClick(View view) {
switch(view.getId()){
case R.id.button_cancel:
mResponse.sendResponse(-1);
getDialog().dismiss();
break;
case R.id.button_skip:
mResponse.sendResponse(0);
getDialog().dismiss();
break;
case R.id.button_start_break:
mResponse.sendResponse(1);
getDialog().dismiss();
break;
default:
break;
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try{
mResponse = (sendResponse) getActivity();
}catch (ClassCastException e){
System.out.println("Error: " + e);
}
}
}
when the time is up and the app is NOT in the foreground the dialog is still launched
You should not be doing this since this will interrupt the user from doing other work which they might be doing at that time and this can be irritating.
What can I possibly do so that the dialog is launched when the app is not in the foreground?
You can show the dialog only the user is actively using your application or you can fall back to show a notification when the user is not using the app.
And if you desperately want to show a dialog, you can try a dialog themed activity
I have developed an app that has two buttons (left and right) and a Textview that will pop up on the screen.Each button has it's corresponding word.
The user has to click the button that corresponds to TextView's word as quickly as possible when it shows. I want to calculate it's reaction time on clicking the button.
Below is my code.
public class Place_to_go_1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_place_to_go_1);
placeone = Global_variables.getFirst_choice_label();
placetwo = Global_variables.getSecond_choice_label();
p_one = (TextView)findViewById(R.id.p_one);
p_two = (TextView)findViewById(R.id.p_two);
btnleft = (ImageButton)findViewById(R.id.btnleft);
btnright = (ImageButton)findViewById(R.id.btnright);
next = (ImageButton)findViewById(R.id.Next);
lblmaintext = (TextView)findViewById(R.id.lblmaintext);
lblprompt = (TextView)findViewById(R.id.lblprompt);
lblreact = (TextView)findViewById(R.id.lblreact);
imgmain = (ImageView)findViewById(R.id.imgmain);
//prac = (ImageView) findViewById(R.id.prac);
Intent intent = getIntent();
final String randomId = intent.getStringExtra("Info_id");
//============ validate image if not empty
setImage_onLaunch();
//==== populate left and right choices===
populate_headers(placeone, placetwo);
//==== populate attributes=====
populate_attributes();
//============== instruction ======
setInstruction();
//=============media
wrong_press = MediaPlayer.create(this, R.raw.wrong_press);
react_fast = MediaPlayer.create(this, R.raw.react_faster);
//=== left button click trigger
btnleft.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String position = "H";
if (tanan[counter].equals(p_one.getText().toString())) {
lblprompt.setVisibility(View.INVISIBLE);
HashMap<String,String> queryValues = new HashMap<String, String>();
queryValues.put("Info_id",randomId);
queryValues.put("Choice",p_one.getText().toString());
queryValues.put("Reaction_time",String.valueOf(elapsedTime));
queryValues.put("Position",position);
queryValues.put("Main",main);
queryValues.put("Error",error);
mydb.insertTest(queryValues);
counter++;
if (counter < tanan.length) {
btnleft.setEnabled(false);
btnright.setEnabled(false);
timeStamp = System.currentTimeMillis();
//Toast.makeText(Place_to_go_1.this, ""+timeStamp, Toast.LENGTH_SHORT).show();
getreactionTime(p_one.getText().toString(), String.valueOf((((timeStamp) / 1000.0) - ((timeRun) / 1000.0))));
setIntervalTime();
} else {
//======end sa data
postEnd();
}
} else {
// Toast.makeText(Place_to_go_1.this, "Wrong pressed", Toast.LENGTH_SHORT).show();
//wrong_press.start();
wrong_click_audio();
error = "1";
lblprompt.setVisibility(View.VISIBLE);
}
}
});
//==== right button click trigger
btnright.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String position = "A";
if (tanan[counter].equals(p_two.getText().toString())) {
lblprompt.setVisibility(View.INVISIBLE);
HashMap<String,String> queryValues = new HashMap<String, String>();
queryValues.put("Info_id",randomId);
queryValues.put("Choice",p_two.getText().toString());
queryValues.put("Reaction_time", String.valueOf(elapsedTime));
queryValues.put("Position",position);
queryValues.put("Main",main);
queryValues.put("Error",error);
mydb.insertTest(queryValues);
counter++;
if (counter < tanan.length) {
btnleft.setEnabled(false);
btnright.setEnabled(false);
timeStamp = System.currentTimeMillis();
//Toast.makeText(Place_to_go_1.this, ""+timeStamp, Toast.LENGTH_SHORT).show();
getreactionTime(p_two.getText().toString(), String.valueOf((((timeStamp) / 1000.0) - ((timeRun) / 1000.0))));
setIntervalTime();
} else {
//======end sa data
postEnd();
}
} else {
// Toast.makeText(Place_to_go_1.this, "Wrong pressed", Toast.LENGTH_SHORT).show();
// wrong_press.start();
wrong_click_audio();
error = "1";
lblprompt.setVisibility(View.VISIBLE);
}
}
});
// ==== next button for the next activity (Place to go 2)
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = getIntent();
String randomId = intent.getStringExtra("Info_id");
//============= launch activity 2 for place to go
if (instruct == true) {
next.setVisibility(View.INVISIBLE);
// prac.setVisibility(View.VISIBLE);
CountDownTimer();
} else {
//Toast.makeText(getApplication(),"Saved Successfully.",Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(), Place_to_go_2.class);
i.putExtra("Info_id", randomId);
startActivity(i);
}
}
});
}
public void interval(){
if(counter < tanan.length){
lblmaintext.setVisibility(View.VISIBLE);
timeRun = System.currentTimeMillis();
btnleft.setEnabled(true);
btnright.setEnabled(true);
lblmaintext.setText(tanan[counter]);
setImage();
imgmain.setVisibility(View.VISIBLE);
react = true;
reacFaster();
}else{
//======end sa data
Toast.makeText(Place_to_go_1.this, "End data", Toast.LENGTH_SHORT).show();
lblmaintext.setVisibility(View.VISIBLE);
lblmaintext.setText("Ok for now");
}
}
public void setIntervalTime(){
react = false;
lblreact.setVisibility(View.INVISIBLE);
reactFaster_timer.cancel();
lblmaintext.setVisibility(View.INVISIBLE);
lblreact.setVisibility(View.INVISIBLE);
imgmain.setVisibility(View.INVISIBLE);
timer = new CountDownTimer(Global_variables.interval_time_before_choices_will_show,Global_variables.interval_time_before_choices_will_show) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
interval();
}
}.start();
}
int counter_countdown = 0;
int drawwable_amber = R.drawable.amber;
String arr[] = {"Ready...","Set...","Start."};
public void CountDownTimer(){
btnleft.setVisibility(View.INVISIBLE);
btnright.setVisibility(View.INVISIBLE);
lblmaintext.setBackgroundResource(0);
timer = new CountDownTimer(4000,1000) {
#Override
public void onTick(long millisUntilFinished) {
lblmaintext.setTextSize(35);
lblmaintext.setText(arr[counter_countdown]);
counter_countdown++;
}
#Override
public void onFinish() {
btnleft.setVisibility(View.VISIBLE);
btnright.setVisibility(View.VISIBLE);
lblmaintext.setBackgroundResource(drawwable_amber);
// lblmaintext.setText(tanan[counter]);
//setImage();
val_first_launch();
timeRun = System.currentTimeMillis();
react = true;
reacFaster();
}
}.start();
}
public void reacFaster(){
reactFaster_timer = new CountDownTimer(Global_variables.reaction_time_first_param,Global_variables.reaction_time_second_param) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
if(react == true){
//Toast.makeText(Place_to_go_1.this, "please react faster", Toast.LENGTH_SHORT).show();
react_fast.start();
lblreact.setVisibility(View.VISIBLE);
}
}
}.start();
}
public void populate_headers(String one,String two){
//== this methos sets headers as random==//
headers = new ArrayList<String>();
headers.add(one);
headers.add(two);
Collections.shuffle(headers);
p_one.setText(headers.get(0));
p_two.setText(headers.get(1));
}
public void populate_attributes(){
attributes = new ArrayList<String>();
for(int h =0;h < 5;h++){
attributes.add(placeone);
attributes.add(placetwo);
}
Collections.shuffle(attributes);
tanan = new String[attributes.size()];
for(int k = 0; k < tanan.length;k++ ){
tanan[k] = attributes.get(k);
}
}
public void postEnd(){
instruct = false;
lblprompt.setVisibility(View.INVISIBLE);
btnright.setVisibility(View.INVISIBLE);
btnleft.setVisibility(View.INVISIBLE);
next.setVisibility(View.VISIBLE);
lblmaintext.setBackgroundResource(0);
lblmaintext.setTextSize(20);
p_one.setVisibility(View.INVISIBLE);
p_two.setVisibility(View.INVISIBLE);
imgmain.setVisibility(View.INVISIBLE);
reactFaster_timer.cancel();
lblreact.setVisibility(View.INVISIBLE);
lblmaintext.setText("Well done!\nNext, is the main task. It is exactly the same as before but this time words will appear on the screen that might distract you. \nPlease respond as quickly as you can.\n Press Next to begin");
}
//=========== validate if image is enabled/ disble if not set
public void setImage_onLaunch(){
if(Global_variables.getFirst_choice_image().equals("") || Global_variables.getSecond_choice_image().equals("")){
disbaleImage();
}else{
}
}
public void setImage(){
/* if(tanan[counter].equals(p_one.getText().toString())){
imgmain.setImageBitmap(BitmapFactory.decodeFile(Global_variables.getFirst_choice_image()));
}else{
imgmain.setImageBitmap(BitmapFactory.decodeFile(Global_variables.getSecond_choice_image()));
}*/
if(placeone.equals(tanan[counter])){
imgmain.setImageBitmap(BitmapFactory.decodeFile(Global_variables.getFirst_choice_image()));
}else{
imgmain.setImageBitmap(BitmapFactory.decodeFile(Global_variables.getSecond_choice_image()));
}
}
public void val_first_launch(){
if(Global_variables.getFirst_choice_image().equals("") || Global_variables.getSecond_choice_image().equals("")){
lblmaintext.setVisibility(View.VISIBLE);
lblmaintext.setText(tanan[counter]);
}else{
imgmain.setVisibility(View.VISIBLE);
if(placeone.equals(tanan[counter])){
imgmain.setImageBitmap(BitmapFactory.decodeFile(Global_variables.getFirst_choice_image()));
}else{
imgmain.setImageBitmap(BitmapFactory.decodeFile(Global_variables.getSecond_choice_image()));
}
}
}
public void disbaleImage(){
imgmain.setBackgroundResource(0);
imgmain.setVisibility(View.GONE);
}
#Override
public void onBackPressed() {
super.onBackPressed();
startActivity(new Intent(getApplication(), MainActivity.class));
finish();
}
public String getreactionTime(String domain, String time){
// Toast.makeText(Place_to_go_1.this, time, Toast.LENGTH_SHORT).show();
//== get reaction time to every activity
Global_variables.set_timeStamps(domain, time);
return domain;
}
//===== prompt instruction====
public void setInstruction(){
btnleft.setVisibility(View.INVISIBLE);
btnright.setVisibility(View.INVISIBLE);
lblmaintext.setBackgroundResource(0);
lblmaintext.setTextSize(20);
lblmaintext.setText("Instruction:\n\nIf " + p_one.getText().toString() + " appears, press arrow left.\n If " + p_two.getText().toString() +
" appears, press arrow right.\n\nRespond as quickly as you can.");
next.setVisibility(View.VISIBLE);
}
//===== prompt instruction====
public void wrong_click_audio(){
wrong_press.start();
}
//=============end class====================
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
// Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
//Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
}
Here a simple logic to calculate reaction time is to create a variable which hold a time when a question is popped up to user and the time when a user show a click reaction to question and calculate the time difference between these two action.
long timeWhenQuestionShowed = System.currentTimeMillis();
long timeWhenUserReacted = System.currentTimeMillis();
long reactionTime = timeWhenQuestionShowed - timeWhenUserReacted;
This should help:
Try using onTouch instead of onClick.
long timeBefor=0;
long timeReaction=0;
btnleft.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: // when pressed
timeBefore=System.currentTimeMillis();
break;
case MotionEvent.ACTION_UP: // when released
timeReaction=System.currentTimeMillis() - timeBefore; // calculate difference
break;
}
}
timeReaction is your desired value.
The idea is to calculate the difference between 2 points in time. I will write 2 examples of calculating time difference in Java / measuring reaction time in Java:
System.nanoTime() or System.currentTimeMillis()
Differences are discussed here: Time measuring overhead in Java
long endTimeNanoSec = 0;
long startTimeNanoSec = System.nanoTime();
myWorkThatNeedsTiming(); // wait for user button press here
endTimeNanoSec = System.nanoTime();
long totalWorkTimeNanos = endTimeNanoSec - startTimeNanoSec;
Java StopWatch
JavaDoc: StopWatch
Stopwatch stopwatch = Stopwatch.createStarted();
myWorkThatNeedsTiming(); // wait for user button press here
stopwatch.stop();
long totalWorkTimeMillis = stopwatch.elapsedMillis();
It was working fine when it was just one timer but when I added inserted a switch and added two more timers, the stop timer button seized working. All the code is pasted below:
public class timer extends Activity implements OnClickListener {
private Button buttonStartTime, buttonStopTime, buttonStartTime2, buttonStopTime2, buttonStartTime3, buttonStopTime3;
private EditText edtTimerValue, edtTimerValue2, edtTimerValue3;
private TextView textViewShowTime, textViewShowTime2, textViewShowTime3; // will show the time
private CountDownTimer countDownTimer, countDownTimer2, countDownTimer3; // built in android class
// CountDownTimer
private long totalTimeCountInMilliseconds, totalTimeCountInMilliseconds2, totalTimeCountInMilliseconds3; // total count down time in
// milliseconds
private long timeBlinkInMilliseconds, timeBlinkInMilliseconds2, timeBlinkInMilliseconds3; // start time of start blinking
private boolean blink, blink2, blink3; // controls the blinking .. on and off
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer);
buttonStartTime = (Button) findViewById(R.id.btnStartTime);
buttonStopTime = (Button) findViewById(R.id.btnStopTime);
textViewShowTime = (TextView) findViewById(R.id.tvTimeCount);
edtTimerValue = (EditText) findViewById(R.id.edtTimerValue);
buttonStartTime2 = (Button) findViewById(R.id.btnStartTime2);
buttonStopTime2 = (Button) findViewById(R.id.btnStopTime2);
textViewShowTime2 = (TextView) findViewById(R.id.tvTimeCount2);
edtTimerValue2 = (EditText) findViewById(R.id.edtTimerValue2);
buttonStartTime3 = (Button) findViewById(R.id.btnStartTime3);
buttonStopTime3 = (Button) findViewById(R.id.btnStopTime3);
textViewShowTime3 = (TextView) findViewById(R.id.tvTimeCount3);
edtTimerValue3 = (EditText) findViewById(R.id.edtTimerValue3);
buttonStartTime.setOnClickListener(this);
buttonStopTime.setOnClickListener(this);
buttonStartTime2.setOnClickListener(this);
buttonStopTime2.setOnClickListener(this);
buttonStartTime3.setOnClickListener(this);
buttonStopTime3.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnStartTime:
if (v.getId() == R.id.btnStartTime) {
textViewShowTime.setTextAppearance(getApplicationContext(),
R.style.normalText);
setTimer();
buttonStopTime.setVisibility(View.VISIBLE);
buttonStartTime.setVisibility(View.GONE);
edtTimerValue.setVisibility(View.GONE);
edtTimerValue.setText("");
startTimer();
} else if (v.getId() == R.id.btnStopTime) {
countDownTimer.cancel();
buttonStartTime.setVisibility(View.VISIBLE);
buttonStopTime.setVisibility(View.GONE);
edtTimerValue.setVisibility(View.VISIBLE);
}
break;
case R.id.btnStartTime2:
if (v.getId() == R.id.btnStartTime2) {
textViewShowTime2.setTextAppearance(getApplicationContext(),
R.style.normalText);
setTimer2();
buttonStopTime2.setVisibility(View.VISIBLE);
buttonStartTime2.setVisibility(View.GONE);
edtTimerValue2.setVisibility(View.GONE);
edtTimerValue2.setText("");
startTimer2();
} else if (v.getId() == R.id.btnStopTime2) {
countDownTimer2.cancel();
buttonStartTime2.setVisibility(View.VISIBLE);
buttonStopTime2.setVisibility(View.GONE);
edtTimerValue2.setVisibility(View.VISIBLE);
}
break;
case R.id.btnStartTime3:
if (v.getId() == R.id.btnStartTime3) {
textViewShowTime3.setTextAppearance(getApplicationContext(),
R.style.normalText);
setTimer3();
buttonStopTime3.setVisibility(View.VISIBLE);
buttonStartTime3.setVisibility(View.GONE);
edtTimerValue3.setVisibility(View.GONE);
edtTimerValue3.setText("");
startTimer3();
} else if (v.getId() == R.id.btnStopTime) {
countDownTimer3.cancel();
buttonStartTime3.setVisibility(View.VISIBLE);
buttonStopTime3.setVisibility(View.GONE);
edtTimerValue3.setVisibility(View.VISIBLE);
}
break;
}
}
private void setTimer() {
int time = 0;
if (!edtTimerValue.getText().toString().equals("")) {
time = Integer.parseInt(edtTimerValue.getText().toString());
} else
Toast.makeText(timer.this, "Please Enter Minutes...",
Toast.LENGTH_LONG).show();
totalTimeCountInMilliseconds = 60 * time * 1000;
timeBlinkInMilliseconds = 30 * 1000;
}
private void startTimer() {
countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) {
// 500 means, onTick function will be called at every 500
// milliseconds
#Override
public void onTick(long leftTimeInMilliseconds) {
long seconds = leftTimeInMilliseconds / 1000;
if (leftTimeInMilliseconds < timeBlinkInMilliseconds) {
textViewShowTime.setTextAppearance(getApplicationContext(),
R.style.blinkText);
// change the style of the textview .. giving a red
// alert style
if (blink) {
textViewShowTime.setVisibility(View.VISIBLE);
// if blink is true, textview will be visible
} else {
textViewShowTime.setVisibility(View.INVISIBLE);
}
blink = !blink; // toggle the value of blink
}
textViewShowTime.setText(String.format("%02d", seconds / 60)
+ ":" + String.format("%02d", seconds % 60));
// format the textview to show the easily readable format
}
#Override
public void onFinish() {
// this function will be called when the timecount is finished
textViewShowTime.setText("Time up!");
textViewShowTime.setVisibility(View.VISIBLE);
buttonStartTime.setVisibility(View.VISIBLE);
buttonStopTime.setVisibility(View.GONE);
edtTimerValue.setVisibility(View.VISIBLE);
}
}.start();
}
private void setTimer2() {
int time = 0;
if (!edtTimerValue2.getText().toString().equals("")) {
time = Integer.parseInt(edtTimerValue2.getText().toString());
} else
Toast.makeText(timer.this, "Please Enter Minutes...",
Toast.LENGTH_LONG).show();
totalTimeCountInMilliseconds2 = 60 * time * 1000;
timeBlinkInMilliseconds2 = 30 * 1000;
}
private void startTimer2() {
countDownTimer2 = new CountDownTimer(totalTimeCountInMilliseconds2, 500) {
// 500 means, onTick function will be called at every 500
// milliseconds
#Override
public void onTick(long leftTimeInMilliseconds) {
long seconds = leftTimeInMilliseconds / 1000;
if (leftTimeInMilliseconds < timeBlinkInMilliseconds2) {
textViewShowTime2.setTextAppearance(getApplicationContext(),
R.style.blinkText);
// change the style of the textview .. giving a red
// alert style
if (blink2) {
textViewShowTime2.setVisibility(View.VISIBLE);
// if blink is true, textview will be visible
} else {
textViewShowTime2.setVisibility(View.INVISIBLE);
}
blink2 = !blink; // toggle the value of blink
}
textViewShowTime2.setText(String.format("%02d", seconds / 60)
+ ":" + String.format("%02d", seconds % 60));
// format the textview to show the easily readable format
}
#Override
public void onFinish() {
// this function will be called when the timecount is finished
textViewShowTime2.setText("Time up!");
textViewShowTime2.setVisibility(View.VISIBLE);
buttonStartTime2.setVisibility(View.VISIBLE);
buttonStopTime2.setVisibility(View.GONE);
edtTimerValue2.setVisibility(View.VISIBLE);
}
}.start();
}
private void setTimer3() {
int time = 0;
if (!edtTimerValue3.getText().toString().equals("")) {
time = Integer.parseInt(edtTimerValue3.getText().toString());
} else
Toast.makeText(timer.this, "Please Enter Minutes...",
Toast.LENGTH_LONG).show();
totalTimeCountInMilliseconds3 = 60 * time * 1000;
timeBlinkInMilliseconds3 = 30 * 1000;
}
private void startTimer3() {
countDownTimer3 = new CountDownTimer(totalTimeCountInMilliseconds3, 500) {
// 500 means, onTick function will be called at every 500
// milliseconds
#Override
public void onTick(long leftTimeInMilliseconds3) {
long seconds = leftTimeInMilliseconds3 / 1000;
if (leftTimeInMilliseconds3 < timeBlinkInMilliseconds3) {
textViewShowTime3.setTextAppearance(getApplicationContext(),
R.style.blinkText);
// change the style of the textview .. giving a red
// alert style
if (blink3) {
textViewShowTime3.setVisibility(View.VISIBLE);
// if blink is true, textview will be visible
} else {
textViewShowTime3.setVisibility(View.INVISIBLE);
}
blink3 = !blink; // toggle the value of blink
}
textViewShowTime3.setText(String.format("%02d", seconds / 60)
+ ":" + String.format("%02d", seconds % 60));
// format the textview to show the easily readable format
}
#Override
public void onFinish() {
// this function will be called when the timecount is finished
textViewShowTime3.setText("Time up!");
textViewShowTime3.setVisibility(View.VISIBLE);
buttonStartTime3.setVisibility(View.VISIBLE);
buttonStopTime3.setVisibility(View.GONE);
edtTimerValue3.setVisibility(View.VISIBLE);
}
}.start();
}
}
You need to switch on stopping buttons as well.
switch (v.getId()) {
case R.id.btnStartTime:
means v.getId() == R.id.btnStartTime inside this case so checking the value of v.getId() there is useless. The first if (v.getId() == R.id.btnStartTime) will always be true, the second one (v.getId() == R.id.btnStopTime) always false.
I've searched everywhere for an answer to this dilemma. A CountDownTimer(bsCountDownTimer) will not start when it should. When I click the button, it is supposed to begin the onTick(), correct? However, only after I navigate to a different activity and navigate backwards using the UP button, or exit the app or destroy it and restart it does the onTick() start updating the text and sending information to LogCat like it is told to do.
My best guess is that this problem is exclusive to either the CDT sub-class, onBsButtonClick(), onCreate(), or possible onResume()/onPause().
Here is some of the source code.
public class HomeActivity extends Activity {
#SuppressLint("NewApi") #Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
if(bsTimeStamp!=0){bsIsRunning=true;}
if(bsTimeStamp==0){bsIsRunning=false;}
lastFed=(TextView)findViewById(R.id.lastFedTextView);
feedAt=(TextView)findViewById(R.id.feedAtTextView);
daysText = (EditText)findViewById(R.id.editTextDays);
hoursText = (EditText)findViewById(R.id.editTextHours);
minutesText = (EditText)findViewById(R.id.editTextMinutes);
daysText.setText("");
hoursText.setText("");
minutesText.setText("");
// get timeStamp and boolean from onFeedButtonClick() START
final SharedPreferences prefs = this.getPreferences(Context.MODE_PRIVATE);
feedClickTimeStamp = prefs.getLong("savedOnFeedClick", placeholder);
bsTimeStamp = prefs.getLong("savedOnBsClick", bsProgress);
bsPlaceholder = prefs.getLong("saved placeholder", bsPlaceholder);
bsIsRunning = prefs.getBoolean("bs is running", bsIsRunning);
progressbs = Long.toString(bsProgress);
placeholderbs = Long.toString(bsPlaceholder);
timestampbs = Long.toString(bsTimeStamp);
timestamp = Long.toString(feedClickTimeStamp);
LAST_FED = prefs.getString("lastFed", LAST_FED);
FEED_AT = prefs.getString("feedAt", FEED_AT);
feedAt.setText("Feed at: " + FEED_AT);
lastFed.setText("Last fed at: " + LAST_FED);
// get timeStamp and boolean from onFeedButtonClick() END
DateTime date = new DateTime();
long currentTime = date.getMillis();
Long bsDiffInMillis;
if(bsIsRunning=false) {
bsDiffInMillis = 0L;
}
else {
bsDiffInMillis = currentTime - bsTimeStamp;
bsPlaceholder -= bsDiffInMillis;
}
Integer bsDiffInt = Integer.valueOf(bsDiffInMillis.intValue());
int roundedDiff = (bsDiffInt + 500) / 1000 * 1000;
j += roundedDiff - 2000;
// BS PROGRESS BAR START
bsProgressBar = (ProgressBar)findViewById(R.id.bsProgressBar);
bsProgressBar.setProgress(j);
Long bsPlaceholderLong = bsPlaceholder;
final Integer setMax = Integer.valueOf(bsPlaceholderLong.intValue());
bsProgressBar.setMax(setMax);
setProgressBarVisibility(true);
if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB){
bsProgressBar.setRotation(180);
} else{
// FIND A WAY TO ROTATE PROGRESSBAR BEFORE API 11 (3.0)
}
timeDisplayBs=(TextView)findViewById(R.id.bs_countdown); ((TextView)findViewById(R.id.bs_countdown)).setText(convertMillisForCrafting(bsPlaceholder-ji));
millisInFuture = bsPlaceholder;
bsCountDownTimer = new CDT(millisInFuture, countDownInterval);
// START BS BUTTON LISTENER //
final Button startBsBtn = (Button) findViewById(R.id.bsButton);
startBsBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBsButtonClick(view);
} //close onClick
}); //close onClickListener
// END BS BUTTON LISTENER //
if (savedInstanceState != null) {} else {}
}// onCreate END
#Override
protected void onPause() {
super.onPause();
bsCountDownTimer.cancel();
}
#SuppressLint("NewApi") #Override
protected void onResume() {
super.onResume();
SharedPreferences prefs = this.getPreferences(Context.MODE_PRIVATE);
bsIsRunning = prefs.getBoolean("bs is running", bsIsRunning);
if(feedClickTimeStamp>0){
mountCountDownTimer.start();
}
if(bsIsRunning==true) {
bsCountDownTimer.start();
bsIsRunning=true;
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("bs is running", bsIsRunning).commit();
}
if(bsIsRunning==false){
bsIsRunning=false;
String progressBarTitleBs = "blacksmithing research";
timeDisplayBs = (TextView)findViewById(R.id.bs_countdown);
timeDisplayBs.setText(progressBarTitleBs.toUpperCase(preferredLocale));
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("bs is running", bsIsRunning).commit();
}
}
public class CDT extends CountDownTimer {
public CDT(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
millisInFuture = prefs.getLong("saved placeholder", bsPlaceholder);
timeDisplayBs=(TextView)findViewById(R.id.bs_countdown);
}
#Override
public void onTick(long millisInFuture) {
Log.v("bsTimer", "Tick of Progress " + ji + " " + millisInFuture);
ji+=1;
j+=1000;
bsProgressBar.setProgress(j);
timeDisplayBs.setText(convertMillisForCrafting(millisInFuture-ji));
}
#Override
public void onFinish() {
bsCountDownTimer.cancel();
j=0;
ji=0;
bsPlaceholder = 0;
bsTimeStamp = 0;
bsProgressBar.setProgress(0);
String progressBarTitleBs = "blacksmithing research";
timeDisplayBs = (TextView)findViewById(R.id.bs_countdown);
timeDisplayBs.setText(progressBarTitleBs.toUpperCase(preferredLocale));
}
}
public void onBsButtonClick(View view) {
final SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
bsTimeStamp = prefs.getLong("savedOnBsClick", bsProgress);
bsPlaceholder = prefs.getLong("saved placeholder", bsPlaceholder);
bsIsRunning = prefs.getBoolean("bs is running", bsIsRunning);
EditText daysText = (EditText)findViewById(R.id.editTextDays);
EditText hoursText = (EditText)findViewById(R.id.editTextHours);
EditText minutesText = (EditText)findViewById(R.id.editTextMinutes);
int daysPh;
int hoursPh;
int minutesPh;
String daysStr = daysText.getText().toString();
String hoursStr = hoursText.getText().toString();
String minutesStr = minutesText.getText().toString();
if (daysStr.matches("") && hoursStr.matches("") && minutesStr.matches("")) {
Toast.makeText(this, "You did not enter DAYS, HOURS, or MINUTES.", Toast.LENGTH_LONG).show();
return;
}
if(bsIsRunning==false){
bsPlaceholder = 0;
bsTimeStamp = 0;
bsIsRunning=true;
j=0;
bsProgressBar.setProgress(0);
Long bsPlaceholderLong = bsPlaceholder;
final Integer setMax = Integer.valueOf(bsPlaceholderLong.intValue());
bsProgressBar.setMax(setMax);
if(daysText.getText().toString().equals("")){
daysText.setText("0");
}
if(hoursText.getText().toString().equals("")){
hoursText.setText("0");
}
if(minutesText.getText().toString().equals("")){
minutesText.setText("0");
}
daysPh = Integer.parseInt(daysText.getText().toString());
hoursPh = Integer.parseInt(hoursText.getText().toString());
minutesPh = Integer.parseInt(minutesText.getText().toString());
daysText.setText("");
hoursText.setText("");
minutesText.setText("");
SharedPreferences.Editor editor = prefs.edit();
bsPlaceholder = getMillisForCrafting(daysPh, hoursPh, minutesPh);
millisInFuture = bsPlaceholder; //VITAL
DateTime dt = new DateTime();
bsProgress = dt.getMillis();
editor.putBoolean("bs is running", bsIsRunning).commit();
editor.putLong("savedOnBsClick", bsProgress).commit();
editor.putLong("saved placeholder", bsPlaceholder).commit();
bsCountDownTimer.start();
} //close if bsIsRunning==false
else if(bsIsRunning==true){
view.invalidate();
new AlertDialog.Builder(HomeActivity.this)
.setTitle("New Blacksmithing Research Timer? (erases current)")
.setMessage("Are you sure you want to start a new timer? \n(Current timer will be erased.)")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
bsPlaceholder = 0;
bsTimeStamp = 0;
bsCountDownTimer.cancel();
bsIsRunning=true;
j=0;
bsProgressBar.setProgress(0);
String progressBarTitleBs = "blacksmithing research";
timeDisplayBs = (TextView)findViewById(R.id.bs_countdown);
timeDisplayBs.setText(progressBarTitleBs.toUpperCase(preferredLocale));
EditText daysText = (EditText)findViewById(R.id.editTextDays);
EditText hoursText = (EditText)findViewById(R.id.editTextHours);
EditText minutesText = (EditText)findViewById(R.id.editTextMinutes);
if(daysText.getText().toString().equals("")){
daysText.setText("0");
}
if(hoursText.getText().toString().equals("")){
hoursText.setText("0");
}
if(minutesText.getText().toString().equals("")){
minutesText.setText("0");
}
int daysPh = Integer.parseInt(daysText.getText().toString());
int hoursPh = Integer.parseInt(hoursText.getText().toString());
int minutesPh = Integer.parseInt(minutesText.getText().toString());
SharedPreferences.Editor editor = prefs.edit();
bsPlaceholder = getMillisForCrafting(daysPh, hoursPh, minutesPh);
DateTime dt = new DateTime();
bsProgress = dt.getMillis();
editor.putBoolean("bs is running", bsIsRunning).commit();
editor.putLong("savedOnBsClick", bsProgress).commit();
editor.putLong("saved placeholder", bsPlaceholder).commit();
Long bsPlaceholderLong = bsPlaceholder;
final Integer setMax = Integer.valueOf(bsPlaceholderLong.intValue());
bsProgressBar.setMax(setMax);
daysText.setText("");
hoursText.setText("");
minutesText.setText("");
bsCountDownTimer.start();
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}return;
}
public static Long getMillisForCrafting(int daysPh, int hoursPh, int minutesPh) {
Locale.getDefault();
DateTime bs = new DateTime();
daysPulled = daysPh;
hoursPulled = hoursPh;
minutesPulled = minutesPh;
final long nowInMillis = bs.getMillis();
long days = daysPulled * 86400000;
long hours = hoursPulled * 3600000;
long minutes = minutesPulled * 60000;
long millisToAddToNow = days + hours + minutes;
long futureDateInMillis = millisToAddToNow + nowInMillis;
long millisFromDate = futureDateInMillis - nowInMillis;
return millisFromDate;
}
public void onBsResetButtonClick(View view) {
final SharedPreferences prefs = this.getPreferences(Context.MODE_PRIVATE);
bsTimeStamp = prefs.getLong("savedOnBsClick", bsProgress);
new AlertDialog.Builder(this)
.setTitle("Reset Timer?")
.setMessage("Reset Blacksmithing Research timer? \n(Current timer will be erased.)")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
j=0;
bsProgressBar.setProgress(0);
bsCountDownTimer.cancel();
bsIsRunning=false;
String progressBarTitleBs = "blacksmithing research";
timeDisplayBs = (TextView)findViewById(R.id.bs_countdown);
timeDisplayBs.setText(progressBarTitleBs.toUpperCase(preferredLocale));
bsPlaceholder = 0;
bsTimeStamp = 0;
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("bs is running", bsIsRunning).commit();
editor.putLong("savedOnBsClick", 0).commit();
editor.putLong("saved placeholder", 0).commit();
// CLEAR INPUT EDITTEXT AREAS
EditText daysText = (EditText)findViewById(R.id.editTextDays);
EditText hoursText = (EditText)findViewById(R.id.editTextHours);
EditText minutesText = (EditText)findViewById(R.id.editTextMinutes);
daysText.setText("");
hoursText.setText("");
minutesText.setText("");
// CLEAR INPUT EDITTEXT AREAS
}})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
#SuppressLint("DefaultLocale")
public static String convertMillis(long milliseconds){
long seconds, minutes, hours;
seconds = milliseconds / 1000;
minutes = seconds / 60;
seconds = seconds % 60;
hours = minutes / 60;
minutes = minutes % 60;
Locale.getDefault();
String time = String.format("%02d:%02d:%02d", hours, minutes, seconds);
return(time);
}
#SuppressLint("DefaultLocale")
public static String convertMillisForCrafting(long milliseconds){
long seconds, minutes, hours, days;
seconds = milliseconds / 1000;
minutes = seconds / 60;
seconds = seconds % 60;
hours = minutes / 60;
days = hours / 24;
hours = hours % 24;
minutes = minutes % 60;
Locale.getDefault();
String timeBs = String.format("%02d days %02d hours %02d minutes %02d seconds", days, hours, minutes, seconds);
return(timeBs);
}
#Override
protected void onDestroy() {
super.onDestroy(); // Always call the superclass
mProgressBar.destroyDrawingCache();
mountCountDownTimer.cancel();
// bsProgressBar.destroyDrawingCache();
// bsCountDownTimer.cancel();
// android.os.Debug.stopMethodTracing(); // Stop method tracing that the activity started during onCreate()
}
}
I got it!
The countDownTimer wouldn't start because the variable from onBsButtonClick() (bsPlaceholder) wasn't being passed into onCreate() correctly.
Here's what I did:
Declared a global variable
static long timeInput;
Created a sub-class that extends CountDownTimer:
public class bsTimer extends CountDownTimer {
public bsTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onTick(long millisInFuture) {
Log.v("bsTimer", "Tick of Progress " + ji + " " + millisInFuture);
ji+=1;
j+=1000;
bsProgressBar.setProgress(j);
((TextView)findViewById(R.id.bs_countdown)).setText(convertMillisForCrafting(millisInFuture-ji));
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
}
}
Instantiated the countdown timer in onCreate();
bsCountDownTimer = new bsTimer(timeInput, 1000);
Then just made a new timer and started it in the onBsButtonClick() method!
bsCountDownTimer = new bsTimer(timeInput, 1000).start();
I am having an issure trying to stop both of my runnables with one stop button, i can stop just one of the two just fine but as soon as i try to stop both my app freezes when the button is pressed on my phone, here is my code so far:
if(go != 1){
go = 1;
final Timer t =new Timer();
t.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
if(DHPDPS==0){
money = (DPPS+Reserve);
Reserve = (money);
String end = String.format("%1f", money);
t1.setText("$" + end);
}else if(counter > DHPDPS && DOTPPS != 0 && DHPDPS != 0){
money = (DOTPPS+Reserve);
Reserve = (money);
String end = String.format("%1f", money);
t1.setText("$" + end);
} else{
money = (DPPS+Reserve);
Reserve = (money);
String end = String.format("%1f", money);
t1.setText("$" + end);
}
//Place your stopping condition over here. Its important to have a stopping condition or it will go in an infinite loop.
counter++;
// Display pay per second
if(counter <= DHPDPS || DHPDPS == 0){
t2.setText("Your pay per second is: $"+result);
}else{
t2.setText("Your pay per second is: $"+result2);
}
}
});
}
}, 20, 20);
// Make countdown to overtime display
final TextView count = (TextView) findViewById(R.id.countdown);
countdown = (int)HPDPS;
cd.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run(){
int hours = (countdown/3600);
if(OTPPS != 0 && HPDPS != 0){
count.setText("Seconds Remaining to Overtime: " + countdown + "\nAbout " + hours + " Hours");
countdown--;
}
}
});
}
}, 1000, 1000);
final Button b = (Button) findViewById(R.id.clockout);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(go == 1)
go = 0;
if (t != null)
t.cancel();
// if (cd != null) // This condition Freezes my phone when activated?
// cd.cancel();
}
});
}
}
Any help would be greatly appreciated! thanks.
public void run() {
while(running) {
//your code for both runnables
}
}
final Button b = (Button) findViewById(R.id.clockout);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(running) {
running = false;
}
}
});
With this code, your runnables will check if they should be running each loop, and if not, they will exit their run() and the thread will stop.
I have figured out what the problem was, it turned out to be a really simple fix,
Here is my original code byte:
final Button b = (Button) findViewById(R.id.clockout);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(go == 1)
go = 0;
if (t != null)
t.cancel();
// if (cd != null) // This condition Freezes my phone when activated?
// cd.cancel();
}
Here is what i did to fix the problem:
final Button b = (Button) findViewById(R.id.clockout);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(go == 1)
go = 0;
if (t != null){
t.cancel();
cd.cancel();
}
}
It was extremely simple, just something i looked over i guess, Thanks for everyones help!
1. Create a boolean variable which will be set to true in the beginning...
2. Use this variable in both the runnables like ... while (isOk)
3. On the click of the button, make this boolean variable as false, and both the runnnable
will cease to exists...