Domanda

I need to return the value from my countdowntimer so I can update a textView. I'm not sure how I can do this, I've added a comment where I've tried adding a return but no luck.

public class BrewTimer extends Activity{

        public String secsRemain = "not set";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            System.out.println("Timer Created, we're in the onCreate Method.");
        }

        public void startBrewTimer(long remaningMillis) {
            // Start the Brew Timer
            System.out.println("Timer object fired!");

            new CountDownTimer(remaningMillis, 1000) {

                public void onTick(long millisUntilFinished) {
                    secsRemain = "seconds remaining: " + millisUntilFinished / 1000;
                    System.out.println(secsRemain);

                    return secsRemain; // <-- This is what I want to do
                }

                public void onFinish() {
                    System.out.println("Timer Finished!!!");
                }
            }.start();
        }

        public void stopBrewTimer() {
            //Stop the Brew Timer
        }

        }
È stato utile?

Soluzione

I need to return the value from my countdowntimer so I can update a textView.

There is no need to do that. It runs on the ui thread. You can update textview in onTick itself.

Example:

public class MainActivity extends Activity { 
TextView tv;
  public String secsRemain = "not set";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = (TextView) findViewById(R.id.textView1);
    startBrewTimer(200000);     
}
private void startBrewTimer(long remaningMillis){
    CountDownTimer counter = CountDownTimer(remaningMillis, 1000){
        public void onTick(long millisUntilDone){
        secsRemain = "seconds remaining: " + millisUntilFinished / 1000; 
          tv.setText(secsRemain);                    
        }

        public void onFinish() {
            tv.setText("DONE!");

        }
    }.start();
}
 }

Altri suggerimenti

You can get values through broadcast receiver......as follows, First create your own IntentFilter as,

    Intent intentFilter=new IntentFilter();
    intentFilter.addAction("YOUR_INTENT_FILTER");

Then create inner class BroadcastReceiver as,

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    /** Receives the broadcast that has been fired */
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction()=="YOUR_INTENT_FILTER"){
           //HERE YOU WILL GET VALUES FROM BROADCAST THROUGH INTENT EDIT YOUR TEXTVIEW///////////
           String receivedValue=intent.getStringExtra("KEY");
        }
    }
};

Now Register your Broadcast receiver in onResume() as,

registerReceiver(broadcastReceiver, intentFilter);

And finally Unregister BroadcastReceiver in onDestroy() as,

unregisterReceiver(broadcastReceiver);

Now the most important part...You need to fire the broadcast from wherever you need to send values..... so do as,

Intent i=new Intent();
i.setAction("YOUR_INTENT_FILTER");
i.putExtra("KEY", "YOUR_VALUE");
sendBroadcast(i);

don't forget to accept my answer if you find this ans satisfactory....cheers :)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top