Not sure what you are trying to do. You can use the below for reference.
If you implement Thread or HandlerThread
, be sure that your UI thread does not block while waiting for the worker thread to complete—do not call Thread.wait()
or Thread.sleep()
.
Check this developer.android.com/training/articles/perf-anr.html
You should not block your main ui thread. Instead of a timer you can use a 'Handler'
You can start and stop a timer on button click. You can reschedule your timer with the count value. You need to take care of orientation change as activity is destroyed and recreated. The value of count we will be re-initialized.
MainActivity.java
public class MainActivity extends Activity implements OnClickListener{
Timer t;
int count = 0;
boolean isDone = true;
Button b;
TextView tv;
Thread thread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.textView1);
t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
public void run() {
tv.setText(String.valueOf(count));
if(isDone)
count++;
}
});
}
}, 1000, 1000);
b.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stu
final Dialog d = new Dialog(MainActivity.this);
d.setTitle("Pause Menu");
d.setContentView(R.layout.pausemenu);
Button b1 = (Button) d.findViewById(R.id.button1);
Button b2 = (Button) d.findViewById(R.id.button2);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
t.cancel();
d.dismiss();
}
});
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(isDone)
{
t.cancel();
isDone=false;
}
t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
public void run() {
tv.setText(String.valueOf(count));
count++;
}
});
}
}, 1000, 1000);
d.dismiss();
}
});
d.show();
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="20sp"
android:text="TextView" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/textView1"
android:layout_marginBottom="21dp"
android:text="Button" />
</RelativeLayout>
pausemenu.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="118dp"
android:layout_marginLeft="51dp"
android:text="Stop" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_marginLeft="32dp"
android:layout_toRightOf="@+id/button1"
android:text="Start" />
</RelativeLayout>
Edit:
Using Handler
Handler m_handler;
Runnable m_handlerTask ;
m_handlerTask = new Runnable()
{
@Override
public void run() {
if(isDone)
{
tv.setText(""+count);
count++;
m_handler.postDelayed(m_handlerTask, 1000);
}
// m_handler.removeCallbacks(m_handlerTask);
}
};
m_handlerTask.run();
You should stop the timer and cancel the handler when not required.
Call t.cancel()
if your using timer and use m_handler.removeCallbacks(m_handlerTask)
to stop the handler