// 設定不休眠
getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// 播放音效
SoundPool mySound;
mySound = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
ensId = mySound.load(this, R.raw.ens, 1);
mySound.play(ensId, 1, 1, 1, 0, 1);
// 播放音樂
MediaPlayer mp;
mp = MediaPlayer.create(MainActivity.this, R.raw.amitabha);
if (mp.isPlaying()) { // 播放中
mp.pause(); // 音樂暫停
} else {
mp.start(); // 播放
mp.setLooping(true); // 設定單曲循環播放
}
// 切換到念佛頁面
Button gotoBuddhaPage = (Button)findViewById(R.id.PageToBuddha);
gotoBuddhaPage.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
if (mp.isPlaying()) { mp.stop();
}
Intent intent = new Intent();
intent.setClass(MainActivity.this, MainActivity2.class);
startActivity(intent);
finish();
}
}
);
// 儲存記數記錄
SharedPreferences preferences = getSharedPreferences("preCount3File", MODE_PRIVATE);
preferences.edit().putInt("counter", counter).apply(); // this or submit();
// 讀取記數記錄
counter = preferences.getInt("counter", 0);
// 顯示 記數數值
TextView showCount = (TextView)findViewById(R.id.showCount);
showCount.setText(String.valueOf(++counter));
// 設定按鈕字體顏色
Button PTB;
PTB = (Button)findViewById(R.id.PrayToBuddha);
PTB.setTextColor(Color.BLACK);
PTB.setTextColor(Color.BLUE);
// 設定按鈕背景顏色
Button clickBtn = (Button)findViewById(R.id.click_btn); // 按我
clickBtn.setBackgroundColor(Color.parseColor("#ffffbba8"));
clickBtn.setBackgroundColor(Color.parseColor("#ffa1c9ff"));
// 計時功能
private boolean startflag=false;
private int tsec=0,csec=0,cmin=0,chour=0;
Timer timer01 =new Timer(); //宣告Timer
timer01.schedule(task, 0,1000); //設定Timer(task為執行內容,0代表立刻開始,間格1秒執行一次)
private TimerTask task = new TimerTask(){
@Override
public void run() {
if (startflag){
tsec++; //如果startflag是true則每秒tsec+1
Message message = new Message();
message.what = 1; //傳送訊息1
handler.sendMessage(message);
}
}
}
// 顯示計時
TextView timer = (TextView)findViewById(R.id.tvTimer);
timer.setText(secTimeFormat()); //s字串為00:00:00格式
private String secTimeFormat() {
if (tsec >= 3600) {
chour=tsec/3600;
cmin=(tsec-chour*3600)/60;
csec=tsec%60;
} else {
cmin=tsec/60;
csec=tsec%60;
}
String s="";
if(chour <10 chour="" p="" s="0"> else s=""+chour;
if(cmin <10 cmin="" p="" s="s+"> else s=s+":"+cmin;
if(csec < 10) s=s+":0"+csec;
else s=s+":"+csec;
return s;
}
10>10>