2017-02-25 9 views
0

アラーム音に似たアプリを作成しようとしていますが、これははるかに簡単で、騒々しい音ではなく完全な曲を再生します。私は毎秒更新されている現在の時間を取得することができました。ユーザーが音楽の再生を開始する時間、分、秒、および期間について、一連のスピナーを使用して設定します。しかし、私はそれを動作させることはできません。私はそれに問題のonClickメソッドのコードの位置だと思う現在の時刻がユーザーの設定した時刻と一致すると音楽を再生する

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_time_setter); 

    this.arraySpinner = new String[] { 
      "Song 1", "Song 2", "Song 3", "Song 4", "Song 5" 
    }; 

    this.hrSpin = new String[] { 
      "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" 
    }; 

    this.minSpin = new String[] { 
      "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", 
      "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", 
      "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", 
      "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60" 
    }; 

    this.secSpin = new String[] { 
      "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", 
      "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", 
      "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", 
      "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60" 
    }; 

    this.perSpin = new String[] { 
      "AM", "PM" 
    }; 
    s = (Spinner) findViewById(R.id.songPicker); 
    hr = (Spinner) findViewById(R.id.hrSpinner); 
    min = (Spinner) findViewById(R.id.minSpinner); 
    sec = (Spinner) findViewById(R.id.secSpinner); 
    period = (Spinner) findViewById(R.id.periodSpinner); 
    submit = (Button)findViewById(R.id.subBtn); 

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_spinner_item, arraySpinner); 
    s.setAdapter(adapter); 

    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, 
      android.R.layout.simple_spinner_item, hrSpin); 
    hr.setAdapter(adapter1); 

    ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, 
      android.R.layout.simple_spinner_item, minSpin); 
    min.setAdapter(adapter2); 

    ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(this, 
      android.R.layout.simple_spinner_item, secSpin); 
    sec.setAdapter(adapter3); 

    ArrayAdapter<String> adapter4 = new ArrayAdapter<String>(this, 
      android.R.layout.simple_spinner_item, perSpin); 
    period.setAdapter(adapter4); 


    mediaPlayer = MediaPlayer.create(this, R.raw.test); 

    submit.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      df = new SimpleDateFormat("hh:mm:ss a"); 
      Thread t = new Thread() { 
       @Override 
       public void run() { 
        try { 
         while (!isInterrupted()) { 
          Thread.sleep(1000); 
          runOnUiThread(new Runnable() { 
           @Override 
           public void run() { 
            // update TextView here! 
            current_date = df.format(Calendar.getInstance().getTime()); 
            Toast.makeText(TimeSetter.this, "curr: "+current_date,Toast.LENGTH_LONG).show(); 

           } 
          }); 
         } 
        } catch (InterruptedException e) { 
        } 
       } 
      }; 
      t.start(); 
      new_date = hr.getSelectedItem().toString()+":"+min.getSelectedItem().toString()+":"+sec.getSelectedItem().toString() 
        + " " + period.getSelectedItem().toString(); 
      if(current_date.equals(new_date)) { 
       mediaPlayer.start(); 
      } 
     } 
    }); 

} 

は、ここに私の試みです。私の目標を達成するための提案はありますか?どうもありがとうございました!

答えて

0

SimpleDateFormat inputParser = new SimpleDateFormat(df, Locale.ENGLISH); 

new_date = parseDate(hr.getSelectedItem().toString()+":"+min.getSelectedItem().toString()+":"+sec.getSelectedItem().toString() 
          + " " + period.getSelectedItem().toString(); 

    currentDate= parseDate(current_date); 
    if(currentDate.equals(new_date); 
をお試しください
関連する問題