0

I'm trying to make an android apps which the administractor will set timer count down and the users must be able to see the time count down...I can able to set timer but i dont know how to pass it to another activity...please help here my code

public class Timer extends Activity  {
    public TextView mTextField;

    public String formatTime(long millis) { 
        String output = "00:00"; 
        long seconds = millis / 1000; 
        long minutes = seconds / 60; 

        seconds = seconds % 60; 
        minutes = minutes % 60; 

        String secondsD = String.valueOf(seconds); 
        String minutesD = String.valueOf(minutes); 

        if (seconds < 10) 
          secondsD = "0" + seconds; 
        if (minutes < 10) 
          minutesD = "0" + minutes; 

        output = minutesD + " : " + secondsD; 
        return output; 
      } 
    public CountDownTimer Counter1;

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
         super.onCreate(savedInstanceState); 
         setContentView(R.layout.main); 

//Declare Start/Stop button 
Button btnstart = (Button)findViewById(R.id.button01); 
Button btnstop = (Button)findViewById(R.id.button02); 
Button btnpass = (Button)findViewById(R.id.button03);

//Declare Text fields to show time left 
final TextView mCounter1TextField=(TextView)findViewById(R.id.textView01); 
final TextView mCounter2TextField = (TextView)findViewById(R.id.textView02); 
final TextView mCounter3TextField=(TextView)findViewById(R.id.textView03); 



//Counter 1 
 Counter1 = new CountDownTimer(120000 , 1000) { 
public void onTick(long millisUntilFinished) { 
    mCounter1TextField.setText("Seconds left: " + formatTime(millisUntilFinished)); 
} 

public void onFinish() { 
    mCounter1TextField.setText("Finished!"); 
   // Counter1.start();
} 
}; 

And the activity which I need to Receive timer

public class f extends Activity {
     public TextView tt;

      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.dfsdfsd);

             Timer objtime = new Timer();


          TextView productTitleTextView = (TextView) findViewById(R.id.textViewtimer);

          productTitleTextView.setText("Left Time is : " + objtime.Counter1);


}


}
user1430923
  • 63
  • 3
  • 10

1 Answers1

2

When you say the Admin sets the time, is this happening on another device?

If you want to pass info from one Activity to another in the same app in the same device you can use these two things:

Activity.startActivityForResult (Intent intent, int requestCode) and get the result in the onActivityResult method.

Or you could pass pretty much whatever info you wan from one Activity to another using Extras: putExtra() in the calling activity and getIntent().getExtras() in the receiving activity.

Here is some code that might fit your example: Make sure in your Timer.java class, whatever you are passing to the other f.java Activity is declared globally, not just inside the formatTime, so that you are able to actually pass it, I am guessing when you are pressing the btnpass Button.

If my assumption above is right then this code probably goes inside the onClick() method for the btnpass.

Intent timerIntent = new Intent(this, f.class); //sets your intent to call the f.java class
timerIntent.putExtra("result", output); //puts the String you want to pass
startActivity(timerIntent); //starts the f.java Activity

In the f.java Activity you would get the String output out, I am guessing this would be inside your onCreate() method, like so:

String output = getIntent().getExtras().getString("result"); //gets the output string from your previous Activity
Ralph Pina
  • 761
  • 7
  • 16