Advertisement
Zidinjo

Activity

Nov 2nd, 2015
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. package de.fhflensburg.mani.mittagspause;
  2. /*
  3. Mobile Computing
  4. Raum D 311
  5. Dienstag 08:15 - 09:45
  6. Marvin Meier 570133
  7. Niklas Springhorn 570407
  8. */
  9.  
  10. import android.app.SearchManager;
  11. import android.media.MediaPlayer;
  12. import android.os.CountDownTimer;
  13. import android.support.v7.app.ActionBarActivity;
  14. import android.os.Bundle;
  15. import android.util.Log;
  16. import android.view.Menu;
  17. import android.view.MenuItem;
  18. import android.view.View;
  19. import android.widget.Button;
  20. import android.widget.EditText;
  21. import android.widget.ProgressBar;
  22. import android.widget.TextView;
  23.  
  24.  
  25. public class MainActivity extends ActionBarActivity implements View.OnClickListener {
  26.  
  27. enum ButtonState { // our state of the button
  28. START,
  29. STOP,
  30. ALARM
  31. }
  32. private final static String TAG = "mani fh-Flensburg";
  33. private MediaPlayer mp;
  34. private Button startStopBut;
  35. private TextView showText;
  36. private ProgressBar progress;
  37. private TextView countDown;
  38. private EditText timeField;
  39. private long duration = Long.MAX_VALUE;
  40. private int multiplikator = 1000;
  41. private double counter;
  42. private ButtonState btState = ButtonState.START;
  43. private CountDownTimer ourTimer;
  44.  
  45. @Override
  46. protected void onCreate(Bundle savedInstanceState) {
  47. super.onCreate(savedInstanceState);
  48.  
  49. setContentView(R.layout.activity_main); // the layout main will be added to the activity class
  50. startStopBut = (Button)findViewById(R.id.startStopBut); // deklaration our var
  51. showText = (TextView)findViewById(R.id.showText);
  52. countDown = (TextView)findViewById(R.id.countdown);
  53. timeField = (EditText)findViewById(R.id.timeField);
  54. progress = (ProgressBar)findViewById(R.id.progress);
  55.  
  56. startStopBut.setOnClickListener(this);
  57. mp = MediaPlayer.create(this,R.raw.ring); // creating instanz of mediaplayer with the object referenz and the location of the file
  58. }
  59.  
  60. public void onClick(View v){
  61. if(v.getId() == startStopBut.getId()){ // controll if the button will be clicked
  62. switch(btState)
  63. {
  64. case START:
  65. Log.i(TAG, "STARTtbutclick" + duration);
  66. duration = Long.parseLong(timeField.getText().toString()); // get data from edittext and parse it to a long
  67. initTimer(duration);
  68. progress.setMax((int) duration); // set the maxValue of our progess bar
  69. ourTimer.start(); // start the timer
  70. startStopBut.setText(R.string.butStop);
  71. btState = ButtonState.STOP; // change the state to STOP
  72. break;
  73.  
  74. case STOP:
  75. Log.i(TAG, "STOPbutclick");
  76. ourTimer.cancel();
  77. startStopBut.setText(R.string.butStart);
  78. btState = ButtonState.START;
  79. break;
  80.  
  81. case ALARM:
  82. Log.i(TAG,"ALARMbutclick");
  83. startStopBut.setText(R.string.butStart);
  84. mp.pause();
  85. btState = ButtonState.START;
  86. break;
  87. }
  88. }
  89. }
  90.  
  91. public void initTimer(long seconds)
  92. {
  93. ourTimer = new CountDownTimer(seconds*multiplikator,500) {
  94. @Override
  95. public void onTick(long millisUntilFinished) {
  96. Log.i(TAG,"Countdown Mili "+millisUntilFinished+" "+Math.ceil(millisUntilFinished/(double)multiplikator) );
  97. counter = Math.ceil(millisUntilFinished/(double)multiplikator);
  98. countDown.setText(Double.toString(counter));// -and setText to the widget countDown
  99. progress.setProgress((int)counter); // setProgess to our progressbar
  100. }
  101.  
  102. @Override
  103. public void onFinish() {
  104. Log.i(TAG, "Timer finished");
  105. countDown.setText("0");
  106. progress.setProgress(0);
  107. startStopBut.setText(R.string.alarmOff);
  108. btState = ButtonState.ALARM;
  109. mp.start(); // Sound will be started
  110. }
  111. };
  112. }
  113.  
  114. @Override
  115. public boolean onCreateOptionsMenu(Menu menu) {
  116. // Inflate the menu; this adds items to the action bar if it is present.
  117. getMenuInflater().inflate(R.menu.menu_main, menu);
  118. return true;
  119. }
  120.  
  121. @Override
  122. public boolean onOptionsItemSelected(MenuItem item) {
  123. // Handle action bar item clicks here. The action bar will
  124. // automatically handle clicks on the Home/Up button, so long
  125. // as you specify a parent activity in AndroidManifest.xml.
  126. int id = item.getItemId();
  127.  
  128. //noinspection SimplifiableIfStatement
  129. if (id == R.id.action_settings) {
  130. return true;
  131. }
  132.  
  133. return super.onOptionsItemSelected(item);
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement