简体   繁体   中英

Receiving a string value in in MainActivity from SecondActivity, and then counting it down

In my program you set the string value for the countdown timer in the secondActivity. That value is then sent to the MainActivities textView which is then supposed to start counting down as soon as the value is fetched. Right now I have made it so you can set the value, and the value is fetched correctly, but what I don't know how to do is start Counting Down this value when It is received. I have already made a CounterClass. Here is my MainActivity Code...

Bundle extras = getIntent().getExtras();

    String intentString;
    if(extras != null) {
        intentString = extras.getString("TimeValue");
        timer = new CounterClass(60000, 1000);
        timer.start();
        timeSent();

    } else {
        intentString = "Default String";
    }
    textTime= (TextView) findViewById(R.id.timeText);
    textTime.setText(intentString);

You need to start your timer. Always read the docs .

timer = new CounterClass(millisInFuture, countdownInterval);
timer.start();

EDIT

millisInFuture --- The number of millis in the future from the call to start() until the countdown is done and onFinish() is called.

countDownInterval --- The interval along the way to receive onTick(long) callbacks.

EDIT 18-12-2015

Convert your intentString to millisInFuture and send it to the CounterClass. And then format it back to HH:MM in onTick() method.

String time = "16:54";
String split[] = time.split(":");
long futureInMillis = Integer.parseInt(split[0]) * 60 * 60 * 1000 + Integer.parseInt(split[1]) * 60 * 1000;

Instead of this

TextView timeText = (TextView)findViewById(R.id.timeText);
timeText.setText(intentString);

you need this

textTime= (TextView)findViewById(R.id.timeText);
textTime.setText(intentString);
timer = new CounterClass(10000, 1000);
timer.start();

As you might see timeText has changed to textTime which is a class member variable and your CounterClass uses it.

Lets say that you really want to implement your own class.
I think the best and clean way to do it is by interface.

First create a interface class.
AddTimes.java

public interface AddTimes  {
      void writeTime(String time);
}

Now, implement your interface in your class.

public class MainActivity extends AppCompatActivity implements AddTimes{

Use your class TextView variable "textTime"

   timeText = (TextView)findViewById(R.id.timeText); //remove declaration TextView and use timeText instead of textTime . 

start your class

new CounterClass(5000L,500L, this).start();

At the CounterClass, add constructor.

  AddTimes addTimes;
public CounterClass(long millisInFuture, long countDownInterval, AddTimes addTimes) {
    super(millisInFuture, countDownInterval);
    this.addTimes = addTimes;
}

Replace textTime.setText(hms); to addTimes.writeTime(hms);

Finally. At your main activity. Implement AddTimes method.

public void writeTime(String time) {
    timeText.setText("Time - " + time);
}

Copy the code as it is to you MainActivity.java file.
Hope this might work.

public class MainActivity extends AppCompatActivity {

TextView textTime;
CountDownTimer timer;
int intValue = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle extras = getIntent().getExtras();
if(extras != null) {
    String intentString = extras.getString("TimeValue"); //if this is a plain number like 20, 45, 209, 8
    intValue = Integer.valueOf(intentString);
}
textTime= (TextView)findViewById(R.id.timeText);
timer = new CountDownTimer(intValue * 1000, 1000) {
public void onTick(long millisUntilFinished) {
    intValue--;
    textTime.setText("Count Down: " + intValue);
}
@Override
public void onFinish() {}
}
}
timer.start();
}

You can simply use the count down timer:

  new CountDownTimer(30000, 1000) {

        public void onTick(long millisUntilFinished) {
            textField.setText("seconds remaining: " + millisUntilFinished / 1000);
           //here you can have your logic to set text to edittext
        }

        public void onFinish() {
            textField.setText("done!");
        }

    }.start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM