我正在为Android项目中的闹钟工作,我是编程新手。我的闹钟应用程序正常运行,但它不能用于计划时间,因此如果我将时钟设置为星期日下午12点,它将无效。
public class AlarmClock extends Activity {
private PendingIntent pendingIntent;
private TimePicker alarmTimePicker;
AlarmManager alarmManager;
private EditText descriptionText;
private static MainActivity inst;
private CheckBox checkBox1,checkBox2,checkBox3,checkBox4,checkBox5,checkBox6,checkBox7;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_clock);
alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
checkBox1= (CheckBox) findViewById(R.id.sundayCheckBox);
checkBox2= (CheckBox) findViewById(R.id.mondayCheckBox);
checkBox3= (CheckBox) findViewById(R.id.tuesdayCheckBox);
checkBox4= (CheckBox) findViewById(R.id.wednesdayCheckBox);
checkBox5= (CheckBox) findViewById(R.id.thursdayCheckBox);
checkBox6= (CheckBox) findViewById(R.id.fridayCheckBox);
checkBox7= (CheckBox) findViewById(R.id.saturdayCheckBox);
}
public void save (View v){
if (checkBox1.isChecked())
{
activityToPerform(1);
}
if (checkBox2.isChecked())
{
activityToPerform(2);
}
if (checkBox3.isChecked())
{
activityToPerform(3);
}
if (checkBox4.isChecked())
{
activityToPerform(4);
}
if (checkBox5.isChecked())
{
activityToPerform(5);
}
if (checkBox6.isChecked())
{
activityToPerform(6);
}
if (checkBox7.isChecked())
{
activityToPerform(7);
}
}
private void activityToPerform(int week)
{
Log.d("MainActivity", "Alarm On");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, week);
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
descriptionText= (EditText)findViewById(R.id.descriptionEditText);
Intent myIntent = new Intent(this, AlarmBroadcast.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
}
答案 0 :(得分:0)
启动API 19时,alarmManager.set()
方法未在准确时间传递。请参阅:http://developer.android.com/reference/android/app/AlarmManager.html#set(int,long,android.app.PendingIntent)
您应该使用setExact()
代替