我有这个代码,我做了2个按钮,一个用于设置闹钟,另一个用于检查闹钟响起之前的剩余时间。目前,我正在努力与检查计时器的第二个按钮。有人可以帮助我吗?
这是我的代码:
agg()
感谢您提前
答案 0 :(得分:0)
为了做我的回答并帮助你,我将完成你的代码,并添加一个TextView例如,以便花时间。
public class MainActivity extends AppCompatActivity {
// modify here
private Calendar myTimer = null;
private TextView myTextView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
Button button2 = (Button) findViewById(R.id.button2);
final TextView lefty = (TextView)findViewById(R.id.left);
// modify here
myTextView = (TextView) findViewById(R.id.my_text_view);
myTimer = Calendar.getInstance();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
Long preTime = calendar.getTimeInMillis();
calendar.add(Calendar.HOUR_OF_DAY, 8);
Long postTime = calendar.getTimeInMillis();
Long delay = postTime - preTime;
Intent intent = new Intent(getApplicationContext(), NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
CountDownTimer timer = new CountDownTimer(delay, 1) {
@Override
public void onTick(long millisUntilFinished) {
final int seconds = (int) (millisUntilFinished / 1000) % 60;
final int minutes = (int) ((millisUntilFinished / (1000 * 60)) % 60);
final int hour = (int) ((millisUntilFinished / (1000 * 60 * 60)) % 24);
// modify here
myTimer.set(Calendar.SECOND, seconds);
myTimer.set(Calendar.MINUTE, minutes);
myTimer.set(Calendar.HOUR, hour);
runOnUiThread(new Runnable() {
@Override
public void run() {
lefty.setText(hour + " : " + minutes + " : " + seconds);
}
});
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
}
};
timer.start();
}
});
// modify here
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String myString = new SimpleDateFormat("hh : mm : ss").format(myTimer);
myTextView.setText(myString);
}
});
}
}