Android 中的计划程序到特定日期
Scheduler in Android to a specific date
我正在尝试制作一个计划程序,它会在计划的日期和时间到来时向您发出通知。我正在使用广播接收器 class,我已经让它工作了一天。
这意味着您可以 select 一个月内的任何一天和任何时间它都可以正常工作,但是如果用户下个月或下一年 select 我会遇到很多问题
这是我按计划按钮单击的代码。
这里的 mDay、mMonth、mYear、mHour、mMinute 是用户为他的 select 编辑的日期和时间 activity,cday、cmonth、cyear 是当前的日期和时间。
btn_schedule.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("Entered in Alarm on click");
Calendar cal = Calendar.getInstance();
Hour = cal.get(Calendar.HOUR_OF_DAY);
Minute = cal.get(Calendar.MINUTE);
cday = cal.get(Calendar.DATE);
cmonth = cal.get(Calendar.MONTH);
cyear = cal.get(Calendar.YEAR);
dday = mDay - cday;
dmonth = mMonth - cmonth;
dyear = mYear - cyear;
if (dyear == 0) {
if (dmonth == 0) {
if (dday == 0) {
long CurrentMilli = (mHour * 3600 * 1000) + (mMinute * 60 * 1000);
long SelectedMilli = (Hour * 3600 * 1000) + (Minute * 60 * 1000);
long diff = CurrentMilli - SelectedMilli;
diff1 = diff;
System.out.println("difference and " + diff + "diff1" + diff1);
new CountDownTimer(diff, 1000) { // adjust the
// milli
// seconds
// here
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
}
}.start();
}
} else {// Else if day is not the same
int dh = 24 - Hour; // today's remaining hours till
// next date
int dm = 60 - Minute;// today's remaining minutes
// till next date
long dhm = dh * 3600000;// in milli
long dmm = dm * 60000;// in milli
long totaltm = dhm + dmm;
diff1 = totaltm;
long lh = mHour * 3600000;// selected hours in milli
long lm = mMinute * 60000;// selected min in milli
ltt = lh + lm;
new CountDownTimer(totaltm, 1000) { // adjust the
// milli
// seconds
// here
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
dday--;
forDay1(dday);
}
}.start();
}
} else {
// Else if month is not the same
}
} else {// Else if year is not the same
}
long a = diff1;
Long alertTime = new GregorianCalendar().getTimeInMillis() + a;
Intent alertIntent = new Intent(HomeScreen.this, Alerts.class);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, alertTime, PendingIntent
.getBroadcast(HomeScreen.this, 2, alertIntent,
PendingIntent.FLAG_UPDATE_CURRENT));
System.out.println("Notification is called");
Intent moreinfo = new Intent(HomeScreen.this, HomeScreen.class);
TaskStackBuilder tsb = TaskStackBuilder
.create(getApplicationContext());
tsb.addParentStack(HomeScreen.class);
tsb.addNextIntent(moreinfo);
}
});
这是我的广播class:
public class Alerts extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("Inside Service");
createNotification(context, "Times Up!", "Start Your Activity!",
"Click here to enter the application!");
}
public void createNotification(Context context, String msg, String msgtext,
String msgalert) {
System.out.println("Method inside service is called");
PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(
context, HomeScreen.class), 0);
NotificationCompat.Builder ncb = new NotificationCompat.Builder(context)
.setContentTitle(msg).setContentText(msgtext)
.setTicker(msgalert).setSmallIcon(R.drawable.ic_launcher)
.setDefaults(Notification.DEFAULT_SOUND)
.setDefaults(Notification.DEFAULT_VIBRATE);
ncb.setContentIntent(pi);
ncb.setAutoCancel(true);
NotificationManager nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(2, ncb.build());
}
}
并且此代码适用于一个月内的任何一天。
您不需要执行计算差异 ddat
和大 if (dyear == 0) {
的所有自定义逻辑...只需将 user-selected 字段设置为 Calendar
:
Calendar cal = Calendar.getInstance();
cal.set(mYear, mMonth, mDay, mHour, mMinute);
然后您可以使用新日历的时间戳作为您的提醒时间。
long alertTime = cal.getTimeInMillis();
我正在尝试制作一个计划程序,它会在计划的日期和时间到来时向您发出通知。我正在使用广播接收器 class,我已经让它工作了一天。
这意味着您可以 select 一个月内的任何一天和任何时间它都可以正常工作,但是如果用户下个月或下一年 select 我会遇到很多问题 这是我按计划按钮单击的代码。
这里的 mDay、mMonth、mYear、mHour、mMinute 是用户为他的 select 编辑的日期和时间 activity,cday、cmonth、cyear 是当前的日期和时间。
btn_schedule.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("Entered in Alarm on click");
Calendar cal = Calendar.getInstance();
Hour = cal.get(Calendar.HOUR_OF_DAY);
Minute = cal.get(Calendar.MINUTE);
cday = cal.get(Calendar.DATE);
cmonth = cal.get(Calendar.MONTH);
cyear = cal.get(Calendar.YEAR);
dday = mDay - cday;
dmonth = mMonth - cmonth;
dyear = mYear - cyear;
if (dyear == 0) {
if (dmonth == 0) {
if (dday == 0) {
long CurrentMilli = (mHour * 3600 * 1000) + (mMinute * 60 * 1000);
long SelectedMilli = (Hour * 3600 * 1000) + (Minute * 60 * 1000);
long diff = CurrentMilli - SelectedMilli;
diff1 = diff;
System.out.println("difference and " + diff + "diff1" + diff1);
new CountDownTimer(diff, 1000) { // adjust the
// milli
// seconds
// here
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
}
}.start();
}
} else {// Else if day is not the same
int dh = 24 - Hour; // today's remaining hours till
// next date
int dm = 60 - Minute;// today's remaining minutes
// till next date
long dhm = dh * 3600000;// in milli
long dmm = dm * 60000;// in milli
long totaltm = dhm + dmm;
diff1 = totaltm;
long lh = mHour * 3600000;// selected hours in milli
long lm = mMinute * 60000;// selected min in milli
ltt = lh + lm;
new CountDownTimer(totaltm, 1000) { // adjust the
// milli
// seconds
// here
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
dday--;
forDay1(dday);
}
}.start();
}
} else {
// Else if month is not the same
}
} else {// Else if year is not the same
}
long a = diff1;
Long alertTime = new GregorianCalendar().getTimeInMillis() + a;
Intent alertIntent = new Intent(HomeScreen.this, Alerts.class);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, alertTime, PendingIntent
.getBroadcast(HomeScreen.this, 2, alertIntent,
PendingIntent.FLAG_UPDATE_CURRENT));
System.out.println("Notification is called");
Intent moreinfo = new Intent(HomeScreen.this, HomeScreen.class);
TaskStackBuilder tsb = TaskStackBuilder
.create(getApplicationContext());
tsb.addParentStack(HomeScreen.class);
tsb.addNextIntent(moreinfo);
}
});
这是我的广播class:
public class Alerts extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("Inside Service");
createNotification(context, "Times Up!", "Start Your Activity!",
"Click here to enter the application!");
}
public void createNotification(Context context, String msg, String msgtext,
String msgalert) {
System.out.println("Method inside service is called");
PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(
context, HomeScreen.class), 0);
NotificationCompat.Builder ncb = new NotificationCompat.Builder(context)
.setContentTitle(msg).setContentText(msgtext)
.setTicker(msgalert).setSmallIcon(R.drawable.ic_launcher)
.setDefaults(Notification.DEFAULT_SOUND)
.setDefaults(Notification.DEFAULT_VIBRATE);
ncb.setContentIntent(pi);
ncb.setAutoCancel(true);
NotificationManager nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(2, ncb.build());
}
}
并且此代码适用于一个月内的任何一天。
您不需要执行计算差异 ddat
和大 if (dyear == 0) {
的所有自定义逻辑...只需将 user-selected 字段设置为 Calendar
:
Calendar cal = Calendar.getInstance();
cal.set(mYear, mMonth, mDay, mHour, mMinute);
然后您可以使用新日历的时间戳作为您的提醒时间。
long alertTime = cal.getTimeInMillis();