我正在尝试使用日历通过警报管理器创建通知。我遇到的问题是闹钟在我使用日历设置的时间没有“响起”。
代码:
// Removed imports
public class RemDateFragment extends Fragment implements OnClickListener{
DatePicker datePicker;
Integer day;
Integer month;
Integer year;
// Removed stuff including button, onclicklistener etc
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View RemDate = inflater.inflate(R.layout.rem_date_fragment, container, false);
datePicker = (DatePicker) RemDate.findViewById(R.id.rd_dp);
return RemDate;
}
@Override
public void onClick(View v) {
switch (v.getId()) { // Notification doesn't work properly just yet
case R.id.rd_b_create:
day = datePicker.getDayOfMonth();
month = datePicker.getMonth();
year = datePicker.getYear();
Calendar cal = Calendar.getInstance(); // Didn't work
cal.set(Calendar.DAY_OF_MONTH, day);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.YEAR, year);
cal.set(Calendar.HOUR_OF_DAY, 9);
int vDay = day.intValue();
int vMonth = month.intValue();
int vYear = year.intValue();
if (content_title.length() >=1) {
Intent remIntent = new Intent();
remIntent.setAction("com.RiThBo.noter.remBroadcast");
Bundle extras = new Bundle();
extras.putString("content_title", (String)content_title);
extras.putString("content_text", (String)content_text);
extras.putBoolean("bOngoing", boolean_ongoing);
remIntent.putExtras(extras);
sendBroadcast(remIntent);
PendingIntent remPendingIntent = PendingIntent.getBroadcast(
this.getActivity(), 0, remIntent, 0);
getActivity();
AlarmManager remAlarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE); // The "Context" refers to the "getActivity" in the line above
remAlarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), remPendingIntent); // Instead of 'sleeping' as i was going to do, add the input time to the system current time
Toast.makeText(getActivity(), "Set for " + vDay + "/" + vMonth + "/" + vYear, Toast.LENGTH_SHORT).show(); // Showing that the values are being set correctly
}
}
}
private void sendBroadcast(Intent remIntent) {
// TODO Auto-generated method stub
}
}
广播只是创建一个通知。这适用于我的其他片段,所以不要认为这是问题所在。
谢谢