我正在为约会提醒我创建警报提醒我正在使用以下代码.代码工作得非常好,它向我显示警报提醒,但唯一的问题是,它不区分上午和下午之间的警报,假设我设置警报为上午7点和当前晚上7点在设备中,然后我的警报对话框显示.我怎样才能管理上午和下午?我用这个链接来参考
http://wptrafficanalyzer.in/blog/setting-up-alarm-using-alarmmanager-and-waking-up-screen-and-unlocking-keypad-on-alarm-goes-off-in-android/
AlertDemo.class
import androID.app.AlertDialog; import androID.app.Dialog; import androID.content.DialogInterface; import androID.content.DialogInterface.OnClickListener; import androID.os.Bundle; import androID.support.v4.app.DialogFragment; import androID.vIEw.WindowManager.LayoutParams;public class AlertDemo extends DialogFragment {@OverrIDepublic Dialog onCreateDialog(Bundle savedInstanceState) { /** Turn Screen On and Unlock the keypad when this alert dialog is displayed */ getActivity().getwindow().addFlags(LayoutParams.FLAG_TURN_SCREEN_ON | LayoutParams.FLAG_disMISS_KEyguard); /** Creating a alert dialog builder */ AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); /** Setting Title for the alert dialog */ builder.setTitle("Alarm"); /** Setting the content for the alert dialog */ builder.setMessage("An Alarm by AlarmManager"); /** Defining an OK button event Listener */ builder.setPositivebutton("OK", new OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialog, int which) { /** Exit application on click OK */ getActivity().finish(); } }); /** Creating the alert dialog window */ return builder.create(); } /** The application should be exit, if the user presses the back button */ @OverrIDe public voID onDestroy() { super.onDestroy(); getActivity().finish(); }
}
Appointment.class
import java.text.DateFormatSymbols; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.GregorianCalendar; import androID.app.Activity; import androID.app.AlarmManager; import androID.app.DatePickerDialog; import androID.app.Dialog; import androID.app.Notification; import androID.app.notificationmanager; import androID.app.PendingIntent; import androID.app.TimePickerDialog; import androID.app.TimePickerDialog.OnTimeSetListener; import androID.content.Intent; import androID.os.Bundle; import androID.util.Log; import androID.vIEw.Menu; import androID.vIEw.VIEw; import androID.vIEw.VIEw.OnClickListener; import androID.Widget.button; import androID.Widget.DatePicker; import androID.Widget.TimePicker; import androID.Widget.Toast; public class Appointment extends Activity {button date, time, save;private static final int DIALOG_DATE = 1;private static final int DIALOG_TIME = 2;private int year;private int month;private int day;int i;String strmonth, strday, stryear;String months[] = { "January", "February", "march", "April", "May", "June", "July", "August", "September", "October", "November", "December" };int intmonth, intday, intyear, inthour, intminutes;Calendar c = Calendar.getInstance();private SimpleDateFormat timeFormatter;@OverrIDepublic voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.doctor_appointment); year = c.get(Calendar.YEAR); month = c.get(Calendar.MONTH); day = c.get(Calendar.DAY_OF_MONTH); date = (button) findVIEwByID(R.ID.btnsetdate); time = (button) findVIEwByID(R.ID.btnsettime); save = (button) findVIEwByID(R.ID.btnsave); timeFormatter = new SimpleDateFormat("hh:mm a"); // c.set(Calendar.MONTH, 4); date.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { // Todo auto-generated method stub showDialog(DIALOG_DATE); } }); time.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { // Todo auto-generated method stub showDialog(DIALOG_TIME); } }); save.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { Intent i = new Intent("com.example.healthmanager.DemoActivity"); /** Creating a Pending Intent */ PendingIntent operation = PendingIntent.getActivity( getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK); /** Getting a reference to the System Service ALARM_SERVICE */ AlarmManager alarmManager = (AlarmManager) getBaseContext() .getSystemService(ALARM_SERVICE); String strtime = time.getText().toString(); Log.v("str btntime", strtime); String[] splitstrtime = strtime.split(":"); Log.v("timestr1", splitstrtime[0]); Log.v("timestr2", splitstrtime[1]); int splithour = Integer.parseInt(splitstrtime[0]); String[] splitsecond = splitstrtime[1].split(" "); Log.v("split str second", splitsecond[0]); int splitmin = Integer.parseInt(splitsecond[0]); /** * Creating a calendar object corresponding to the date and time * set by the user */ // GregorianCalendar calendar = new // GregorianCalendar(year,month,day, hour, minute); GregorianCalendar calendar = new GregorianCalendar(intyear, intmonth, intday, splithour, splitmin); /** * Converting the date and time in to milliseconds elapsed since * epoch */ long alarm_time = calendar.getTimeInMillis(); /** Setting an alarm, which invokes the operation at alart_time */ alarmManager .set(AlarmManager.RTC_WAKEUP, alarm_time, operation); /** Alert is set successfully */ Toast.makeText(getBaseContext(), "Alarm is set successfully", Toast.LENGTH_SHORT).show(); } });}// For date dialog@OverrIDeprotected Dialog onCreateDialog(int ID) { switch (ID) { case DIALOG_DATE: return new DatePickerDialog(this, datePickerListener, year, month, day); case DIALOG_TIME: return new TimePickerDialog(this, new OnTimeSetListener() { @OverrIDe public voID onTimeSet(TimePicker vIEw, int hourOfDay, int minute) { c.set(Calendar.HOUR_OF_DAY, hourOfDay); c.set(Calendar.MINUTE, minute); time.setText(timeFormatter.format(c.getTime())); } }, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), false); } return null;}// For dateprivate DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() { public voID onDateSet(DatePicker vIEw, int year1, int monthOfYear, int dayOfMonth) { year = year1; month = monthOfYear; day = dayOfMonth; // date.setText(dateFormatter.format(dateTime.getTime())); updatedisplay(); }};public String getMonthForInt(int m) { String month = "invalID"; DateFormatSymbols dfs = new DateFormatSymbols(); String[] months = dfs.getMonths(); if (m >= 0 && m <= 11) { month = months[m]; } return month;}private voID updatedisplay() { // String strdob = month + 1 + "/" + day + "/" + year; // Log.v("strdob : ", strdob); intmonth = month; intday = day; intyear = year; strmonth = Integer.toString(intmonth); strday = Integer.toString(intday); stryear = Integer.toString(intyear); Log.v("month value", strmonth); Log.v("day value", strday); Log.v("year value", stryear); // int one=7; // Log.v("string limit",one.length()); for (i = 0; i < intmonth; i++) { String strone = Integer.toString(intmonth); strone = months[i]; // String intmonth=Integer.toString(months); } Log.v("month value", months[i].toString()); date.setText(months[i] + " " + day + "," + year);} }
DemoActivity.class
public class DemoActivity extends FragmentActivity {@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /** Creating an Alert Dialog Window */ AlertDemo alert = new AlertDemo(); /** opening the Alert Dialog Window */ alert.show(getSupportFragmentManager(), "AlertDemo");}}
解决方法:
您没有处理AM / PM.只要把这些代码行……
int timeDifference=0; String ampm=splitampmtime[1]; if(ampm.matches("PM")){ timeDifference=12; } int splithour = timeDifference+Integer.parseInt(splitstrtime[0]); String[] splitsecond = splitstrtime[1].split(" ");
总结 以上是内存溢出为你收集整理的android – 如何使用报警管理器区分上午和下午之间的报警?全部内容,希望文章能够帮你解决android – 如何使用报警管理器区分上午和下午之间的报警?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)