Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.fhflensburg.mani.mittagspause;
- /*
- Mobile Computing
- Raum D 311
- Dienstag 08:15 - 09:45
- Marvin Meier 570133
- Niklas Springhorn 570407
- */
- import android.app.SearchManager;
- import android.media.MediaPlayer;
- import android.os.CountDownTimer;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ProgressBar;
- import android.widget.TextView;
- public class MainActivity extends ActionBarActivity implements View.OnClickListener {
- enum ButtonState { // our state of the button
- START,
- STOP,
- ALARM
- }
- private final static String TAG = "mani fh-Flensburg";
- private MediaPlayer mp;
- private Button startStopBut;
- private TextView showText;
- private ProgressBar progress;
- private TextView countDown;
- private EditText timeField;
- private long duration = Long.MAX_VALUE;
- private int multiplikator = 1000;
- private double counter;
- private ButtonState btState = ButtonState.START;
- private CountDownTimer ourTimer;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main); // the layout main will be added to the activity class
- startStopBut = (Button)findViewById(R.id.startStopBut); // deklaration our var
- showText = (TextView)findViewById(R.id.showText);
- countDown = (TextView)findViewById(R.id.countdown);
- timeField = (EditText)findViewById(R.id.timeField);
- progress = (ProgressBar)findViewById(R.id.progress);
- startStopBut.setOnClickListener(this);
- mp = MediaPlayer.create(this,R.raw.ring); // creating instanz of mediaplayer with the object referenz and the location of the file
- }
- public void onClick(View v){
- if(v.getId() == startStopBut.getId()){ // controll if the button will be clicked
- switch(btState)
- {
- case START:
- Log.i(TAG, "STARTtbutclick" + duration);
- duration = Long.parseLong(timeField.getText().toString()); // get data from edittext and parse it to a long
- initTimer(duration);
- progress.setMax((int) duration); // set the maxValue of our progess bar
- ourTimer.start(); // start the timer
- startStopBut.setText(R.string.butStop);
- btState = ButtonState.STOP; // change the state to STOP
- break;
- case STOP:
- Log.i(TAG, "STOPbutclick");
- ourTimer.cancel();
- startStopBut.setText(R.string.butStart);
- btState = ButtonState.START;
- break;
- case ALARM:
- Log.i(TAG,"ALARMbutclick");
- startStopBut.setText(R.string.butStart);
- mp.pause();
- btState = ButtonState.START;
- break;
- }
- }
- }
- public void initTimer(long seconds)
- {
- ourTimer = new CountDownTimer(seconds*multiplikator,500) {
- @Override
- public void onTick(long millisUntilFinished) {
- Log.i(TAG,"Countdown Mili "+millisUntilFinished+" "+Math.ceil(millisUntilFinished/(double)multiplikator) );
- counter = Math.ceil(millisUntilFinished/(double)multiplikator);
- countDown.setText(Double.toString(counter));// -and setText to the widget countDown
- progress.setProgress((int)counter); // setProgess to our progressbar
- }
- @Override
- public void onFinish() {
- Log.i(TAG, "Timer finished");
- countDown.setText("0");
- progress.setProgress(0);
- startStopBut.setText(R.string.alarmOff);
- btState = ButtonState.ALARM;
- mp.start(); // Sound will be started
- }
- };
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement