Results 1 to 7 of 7
Thread: Java thread
Hybrid View
-
2nd December 2017 13:35 #1Registered User
Join Date: Jul:2009
Location: Bulgaria
Posts: 848
Java thread
thread ? , . , - thread . :
, updateTimerText , chronoChange :Code:public class Chronometer implements Runnable { public static final long MILLIS_TO_MINUTES = 60000; private Context mContext; private long mStartTime; private boolean mIsRunning; public Chronometer(Context context) { mContext = context; } public void start() { mStartTime = System.currentTimeMillis(); mIsRunning = true; } public void stop() { mIsRunning = false; } @Override public void run() { while(mIsRunning) { long since = System.currentTimeMillis() - mStartTime; int seconds = (int) (since / 1000) % 60; int minutes = (int) ((since / (MILLIS_TO_MINUTES)) % 60); int millis = (int) (since / 10) % 100; //the last 2 digits of millisecs if(chronoChange == 2){ ((TwoPlayers) mContext).updateTimerText(String.format("%01d:%02d:%02d" , minutes, seconds, millis)); }else if(chronoChange == 1){ ((MainActivity) mContext).updateTimerText(String.format("%01d:%02d:%02d" , minutes, seconds, millis));} try { Thread.sleep(30); } catch (InterruptedException e) { e.printStackTrace(); } } } }
, , thread . thread java thread .. , thread.stop() .Code:public void goWhere(int OneOrTwo) { if(OneOrTwo==1){ chronoChange = 1; Intent intent = new Intent(MainMenu.this, SinglePlayer.class); startActivity(intent); } else{ chronoChange = 2; Intent intnet = new Intent (MainMenu.this, TwoPlayers.class); startActivity(intnet); } }Last edited by atom3; 2nd December 2017 at 13:59.
Alienware 17 R4, i7-6700hq, gtx-1060 OC, 16gb ddr4 2667 ram 256gb NVMe SSD, LSP, MMS, GPRS, UPS, FedEx, ZDR, kopr?, asl, W2F, ... .
-
2nd December 2017 20:30 #2Registered User
Join Date: Mar:2014
Location:
Posts: 16
, Chronometer. Interface ( IChronometerTimeChangeHandler), TimeChanged. activity-, interface. context Chronometer IChronometerTimeChangeHandler private IChronometerTimeChangeHandler mChronometerTimeHandler mChronometerTimeHandler.TimeChanged(Stri ng.format("%01d:%02d:%02d"....).
Chronometer chronometer(this) .
-
2nd December 2017 21:17 #3Registered User
Join Date: Jul:2009
Location: Bulgaria
Posts: 848
:
, threada thread ?Code:mChronometer = new Chronometer(mContext); mThreadChrono = new Thread(mChronometer); mThreadChrono.start(); mChronometer.start(); public void updateTimerText(final String time) { runOnUiThread(new Runnable() { @Override public void run() { time1.setText(time); time2.setText(time); } }); }
thread.stop(); , , thread ?Last edited by atom3; 2nd December 2017 at 23:10.
Alienware 17 R4, i7-6700hq, gtx-1060 OC, 16gb ddr4 2667 ram 256gb NVMe SSD, LSP, MMS, GPRS, UPS, FedEx, ZDR, kopr?, asl, W2F, ... .
-
3rd December 2017 18:19 #4Registered User
Join Date: Mar:2014
Location:
Posts: 16
Android Chronometer, Chronometer. ?
-
3rd December 2017 23:54 #5Registered User
Join Date: Jul:2009
Location: Bulgaria
Posts: 848
-
5th December 2017 18:50 #6Registered User
Join Date: Mar:2014
Location:
Posts: 16
-
:Code:public interface IChronometerTimeChangeHandler { void OnTimeChanged(final int minutes, final int seconds, final int milliseconds); }
Code:public class Chronometer { IChronometerTimeChangeHandler mChronometerTimeChangeHandler; private long mStartTime; private boolean mIsRunning; public static final long MILLIS_TO_MINUTES = 60000; public Chronometer(IChronometerTimeChangeHandler ChronometerTimeChangeHandler) { mStartTime = 0; mIsRunning = false; mChronometerTimeChangeHandler = ChronometerTimeChangeHandler; } public void Start() { if(mIsRunning) return; mIsRunning = true; Thread thread = new Thread(new Runnable() { @Override public void run() { try { CycleTime(); } catch (Exception e) { e.printStackTrace(); } } }); thread.start(); } public void Stop() { mIsRunning = false; } private void CycleTime() { while(mIsRunning) { long since = System.currentTimeMillis() - mStartTime; int seconds = (int) (since / 1000) % 60; int minutes = (int) ((since / (MILLIS_TO_MINUTES)) % 60); int millis = (int) (since / 10) % 100; //the last 2 digits of millisecs mChronometerTimeChangeHandler.OnTimeChanged(minutes,seconds,millis); try { Thread.sleep(30); } catch (InterruptedException e) { e.printStackTrace(); } } } }
:
. .Code:public class MainActivity extends AppCompatActivity implements IChronometerTimeChangeHandler { private Chronometer mChronometer = new Chronometer(this); @Override public void OnTimeChanged(final int minutes,final int seconds,final int milliseconds) { runOnUiThread(new Runnable() { @Override public void run() { String strFormatted = String.format("%01d:%02d:%02d" , minutes, seconds, milliseconds); //... } }); } private void StartChronometer() { mChronometer.Start(); } private void StopChronometer() { mChronometer.Stop(); } }Last edited by Borrowyou; 5th December 2017 at 19:50.




Reply With Quote

Lenovo ThinkPad 15 IdeaPad 15
5th May 2023, 22:16 in