This article introduces the stop and restart implementation code of Android timer Timer. It is shared with you. The details are as follows:
I did a project in July to present an animation using a custom control. I used a timer to control the time, but there was always a problem when it stopped turning on. I have been looking for a reasonable way to solve this problem, but I have not found it. Recently, I have finally found a reasonable way to solve this problem.
How do you query relevant information? You must know that the timer and timetask cancellation method are () and (). However, after you find that this kind of hairstyle is cancelled, you will report an error when you start the timer again.
FATAL EXCEPTION: main Process: .gate_control_scheme, PID: 2472 : Timer already cancelled. at (:397) at (:248) at .gate_control_scheme.(:401) at (:5637) at $(:22429) at (:751) at (:95) at (:154) at (:6119) at (Native Method) at $(:886) at (:776)
The solution to this problem uses cancer(). After canceling the timer, you need to clear the timer. A reasonable code should look like this:
(); mTimer = null; (); mTimerTask = null;
The key problem has been solved, and the following is my case code:
public class MainActivity extends AppCompatActivity { private static String TAG = "TimerDemo"; private TextView mTextView = null; private Button mButton_start = null; private Button mButton_pause = null; private Timer mTimer = null; private TimerTask mTimerTask = null; private Handler mHandler = null; private static int count = 0; private boolean isPause = false; private boolean isStop = true; private static int delay = 1000; //1s private static int period = 1000; //1s private static final int UPDATE_TEXTVIEW = 0; @Override public void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); mTextView = (TextView)findViewById(); mButton_start = (Button)findViewById(.mybutton_start); mButton_pause = (Button)findViewById(.mybutton_pause); mButton_start.setOnClickListener(new () { public void onClick(View v) { if (isStop) { (TAG, "Start"); } else { (TAG, "Stop"); } isStop = !isStop; if (!isStop) { startTimer(); }else { stopTimer(); } if (isStop) { mButton_start.setText(); } else { mButton_start.setText(); } } }); mButton_pause.setOnClickListener(new () { public void onClick(View v) { if (isPause) { (TAG, "Resume"); } else { (TAG, "Pause"); } isPause = !isPause; if (isPause) { mButton_pause.setText(); } else { mButton_pause.setText(); } } }); mHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch () { case UPDATE_TEXTVIEW: updateTextView(); break; default: break; } } }; } private void updateTextView(){ ((count)); } private void startTimer(){ if (mTimer == null) { mTimer = new Timer(); } if (mTimerTask == null) { mTimerTask = new TimerTask() { @Override public void run() { (TAG, "count: "+(count)); sendMessage(UPDATE_TEXTVIEW); do { try { (TAG, "sleep(1000)..."); (1000); } catch (InterruptedException e) { } } while (isPause); count ++; } }; } if(mTimer != null && mTimerTask != null ) (mTimerTask, delay, period); } private void stopTimer(){ if (mTimer != null) { (); mTimer = null; } if (mTimerTask != null) { (); mTimerTask = null; } count = 0; } public void sendMessage(int id){ if (mHandler != null) { Message message = (mHandler, id); (message); } } }
Part of xml code:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android: android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="@string/number" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal" > <Button android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/start" /> <Button android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/pause" /> </LinearLayout> </LinearLayout>
Part of string code:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">TimerDemo</string> <string name="number">0</string> <string name="start">start</string> <string name="stop">stop</string> <string name="pause">pause</string> <string name="resume">resume</string> </resources>
The above is my source code. If you have any questions, you can leave a message to discuss it.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.