Reputation: 136
First of all, Im new to android. So i was displaying the current time on a TextView in the MainActivity.java using a Handler, which worked just fine. Then i used a TimePicker(which is actually in another activity, TimeActivity.java) to pick a time, at which the time stops.
For example, if the time picked using the TimePicker is 8:30, the time in TextView will cease to advance further beyond 8:30
Also theres a STOP button with which calls handler.removeMessages(0); to stop the handler and hence the time.
Here's my MainActivity.java
import static com.gamecodeschool.broadcast.TimeActivity.minute1;
public class MainActivity extends AppCompatActivity {
Button start,stop,set;
TextView textView;
final Handler handler = new Handler();
Runnable r;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Views
textView = (TextView)findViewById(R.id.textView);
start = (Button)findViewById(R.id.button);
stop = (Button)findViewById(R.id.button2);
set = (Button)findViewById(R.id.button3);
set.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent intent = new Intent(getApplicationContext(),TimeActivity.class);
startActivity(intent);
}
});
}
public void stop(View v) {
handler.removeMessages(0);
Toast.makeText(this,"Stopped",Toast.LENGTH_SHORT).show();
}
public void runInBackground(View v) {
handler.post(r);
r = new Runnable() {
@Override
public void run() {
//String time = java.text.DateFormat.getDateTimeInstance().format(new Date());
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
StringBuilder stringBuilder = new StringBuilder().append(hour).append(":").append(minute);
textView.setText(stringBuilder);
if (minute==minute1){
stopTime();
}
handler.postDelayed(this,100);
}
};
}
private void stopTime() {
handler.removeMessages(0);
Toast.makeText(this,"Time Stopped",Toast.LENGTH_SHORT).show();
} }
And Here's my TimeActivity
public class TimeActivity extends AppCompatActivity {
TimePicker timePicker;
Button button;
TextView textView;
StringBuilder stringBuilder;
static int hour1,minute1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time);
timePicker = (TimePicker)findViewById(R.id.timePicker);
button = (Button)findViewById(R.id.saveButton);
textView = (TextView)findViewById(R.id.timeView);
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
showTime(hour,minute);
}
private void showTime(int hour, int minute) {
stringBuilder = new StringBuilder().append(hour).append(":").append(minute);
textView.setText(stringBuilder);
}
public void saveTime(View v){
hour1 = timePicker.getHour();
minute1 = timePicker.getMinute();
showTime(hour1,minute1);
Toast.makeText(TimeActivity.this,hour1+":"+minute1,Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
} }
Upvotes: 0
Views: 785
Reputation: 1077
Updating UI must be done on the UIThread, for instance to update your TextView
you should for example :
public void runInBackground(View v) {
handler.post(r);
r = new Runnable() {
@Override
public void run() {
//String time = java.text.DateFormat.getDateTimeInstance().format(new Date());
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
StringBuilder stringBuilder = new StringBuilder().append(hour).append(":").append(minute);
runOnUiThread(new Runnable() {
@Override public void run() {
textView.setText(stringBuilder);
}
});
textView.setText(stringBuilder);
if (minute==minute1){
stopTime();
}
handler.postDelayed(this,100);
}
};
}
Upvotes: 0