0
我正在嘗試創建類似於鬧鐘的應用程序,但該應用程序要簡單得多,並且會播放完整的歌曲曲目而不是短暫嘈雜的聲音。我能夠獲得每秒更新的當前時間,並且我要求用戶通過一系列旋鈕來設置他們希望音樂開始播放的時間,分鐘,秒和時間段。但是,我無法讓它工作。當前時間與用戶設置的時間匹配時播放音樂
這裏是我的嘗試:
@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();
}
}
});
}
我覺得這個問題是對的onClick方法的代碼的定位。你對如何實現我的目標有任何建議嗎?非常感謝你!