我正在尝试构建一个个人应用程序,它将在 DeskClock 应用程序中设置警报。我可以让它在当天的任何时间设置闹钟,但是我将如何设置第二天或本周晚些时候的闹钟。查看android中的AlarmClock api,我看不到这样做的正常方法。这甚至可能吗?
顺便说一句,这是我设置警报的代码,它可能不漂亮,但我正在学习。
package com.netwokz.setit;
import java.util.Calendar;
import java.util.GregorianCalendar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.AlarmClock;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener {
Button btnSetAlarm;
EditText etHour, etMinute;
int minute, hour, day;
Calendar cal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
btnSetAlarm = (Button) findViewById(R.id.btn_set_alarm);
etHour = (EditText) findViewById(R.id.etHour);
etMinute = (EditText) findViewById(R.id.etMinute);
btnSetAlarm.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_activity, menu);
return true;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_set_alarm:
setAlarm();
break;
}
}
private void setAlarm() {
cal = new GregorianCalendar();
cal.setTimeInMillis(System.currentTimeMillis());
day = cal.get(Calendar.DAY_OF_WEEK);
hour = cal.get(Calendar.HOUR_OF_DAY);
minute = cal.get(Calendar.MINUTE);
Intent i = new Intent(AlarmClock.ACTION_SET_ALARM);
i.putExtra(AlarmClock.EXTRA_HOUR, hour + Integer.parseInt(etHour.getText().toString()));
i.putExtra(AlarmClock.EXTRA_MINUTES, minute + Integer.parseInt(etMinute.getText().toString()));
i.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
startActivity(i);
}
}