- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个使用 AlarmManager 类的闹钟应用程序,它允许用户设置一次性闹钟或重复闹钟。我想扩展功能,以便用户可以避免闹钟响起,例如在周末。
我已经把屏蔽周末闹钟的代码放到了AlarmReceiver.java中。
设置闹钟的AlarmActivity.java代码:
//Set a one time alarm
if (repeatInterval == 0) {
alarmManager.set(AlarmManager.RTC, alarmTime.getTimeInMillis(), pendingIntent);
AlarmReceiver alarmReceiver = new AlarmReceiver(this); //http://stackoverflow.com/questions/16678763/the-method-getapplicationcontext-is-undefined
Toast.makeText(AlarmActivity.this, "Your one time reminder is now set for " + hourSet + ":" + minuteSetString + amPmlabel, Toast
.LENGTH_LONG)
.show();
}
//Set a repeating alarm
else {
alarmManager.setRepeating(AlarmManager.RTC, alarmTime.getTimeInMillis(), repeatIntervalMilliseconds, pendingIntent);
AlarmReceiver alarmReceiver = new AlarmReceiver(this); //http://stackoverflow.com/questions/16678763/the-method-getapplicationcontext-is-undefined
Toast.makeText(AlarmActivity.this, "Your reminder is now set for " + hourSet + ":" + minuteSetString + amPmlabel + " and will " +
"repeat " +
"every " +
repeatInterval + " minutes.", Toast.LENGTH_LONG).show();
}
报警服务.Java:
package com.joshbgold.move.backend;
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import com.joshbgold.move.R;
import com.joshbgold.move.main.AlarmActivity;
public class AlarmService extends IntentService {
private NotificationManager alarmNotificationManager;
public AlarmService() {
super("AlarmService");
}
@Override
public void onHandleIntent(Intent intent) {
sendNotification("Move reminder");
}
private void sendNotification(String msg) {
alarmNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, AlarmActivity.class), 0);
NotificationCompat.Builder alarmNotificationBuilder = new NotificationCompat.Builder(
this).setContentTitle("Reminder").setSmallIcon(R.mipmap.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
alarmNotificationBuilder.setContentIntent(contentIntent);
alarmNotificationManager.notify(1, alarmNotificationBuilder.build());
}
}
AlarmReceiver.Java:
package com.joshbgold.move.backend;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.content.WakefulBroadcastReceiver;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class AlarmReceiver extends WakefulBroadcastReceiver {
Context myContext;
public AlarmReceiver(Context context){
myContext = context;
}
public AlarmReceiver(){
}
//get the current day
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE");
Date date = new Date();
String dayOfTheWeek = simpleDateFormat.format(date);
Calendar calendar = Calendar.getInstance();
int currentHour = calendar.HOUR_OF_DAY;
boolean noWeekends = true;
boolean workHoursOnly = true;
@Override
public void onReceive(final Context context, Intent intent) {
try { //this value could be null if user has not set it...
noWeekends = loadPrefs("noWeekends", noWeekends);
workHoursOnly = loadPrefs("workHoursOnly", workHoursOnly);
} catch (Exception e) {
e.printStackTrace();
}
if(dayOfTheWeek == "Saturday" || dayOfTheWeek == "Sunday" && noWeekends == true) {
//Alarm is not wanted on the weekend
try {
wait(1); //waits for one-thousandth of a millisecond
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else if ((currentHour < 9 || currentHour > 17) && workHoursOnly == true){
//Alarm outside of work hours
try {
wait(1); //waits for one-thousandth of a millisecond
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else {
Intent myIntent = new Intent();
myIntent.setClassName("com.joshbgold.move", "com.joshbgold.move.main.ReminderActivity");
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}
//get prefs
private boolean loadPrefs(String key,boolean value) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(myContext);
boolean data = sharedPreferences.getBoolean(key, value);
return data;
}
}
AlarmReceiver.java(更正后的代码)
package com.joshbgold.move.backend;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.content.WakefulBroadcastReceiver;
import java.util.Calendar;
public class AlarmReceiver extends WakefulBroadcastReceiver {
Context myContext;
public AlarmReceiver(Context context){
myContext = context;
}
public AlarmReceiver() {
}
private boolean workHoursOnly = false;
private boolean noWeekends = false;
@Override
public void onReceive(final Context context, Intent intent) {
Calendar calendar = Calendar.getInstance();
int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
int today = calendar.get(Calendar.DAY_OF_WEEK);
boolean isWeekend = (today == Calendar.SUNDAY) || (today == Calendar.SATURDAY);
boolean isOutsideWorkHours = (currentHour < 9) || (currentHour > 16);
//checkPrefs checks whether a preferences key exists
if (checkPrefs("workHoursOnlyKey")){
workHoursOnly = loadPrefs("workHoursOnlyKey", workHoursOnly);
}
if(checkPrefs("noWeekendsKey")){
noWeekends = loadPrefs("noWeekendsKey", noWeekends);
}
/* try { //this value could be null if user has not set it...
workHoursOnly = loadPrefs("workHoursOnly", workHoursOnly);
} catch (Exception e) {
e.printStackTrace();
}
*/
/*try { //this value could be null if user has not set it...
noWeekends = loadPrefs("noWeekends", noWeekends);
} catch (Exception e) {
e.printStackTrace();
}*/
if(isWeekend && noWeekends) {
//Alarm is not wanted on the weekend
try {
Thread.sleep(1); //waits for millisecond
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else if (isOutsideWorkHours && workHoursOnly){
//Alarm not wanted outside of work hours
try {
Thread.sleep(1); //waits for millisecond
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else {
//Alarm is wanted, and should go off
Intent myIntent = new Intent();
myIntent.setClassName("com.joshbgold.move", "com.joshbgold.move.main.ReminderActivity");
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}
//check if a prefs key exists
private boolean checkPrefs(String key){
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(myContext);
boolean exists = sharedPreferences.contains(key);
return exists;
}
//get prefs
private boolean loadPrefs(String key,boolean value) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(myContext);
boolean data = sharedPreferences.getBoolean(key, value);
return data;
}
}
最佳答案
您的方法很好。无论如何,我建议您避免使用硬编码字符串进行周末检查。您已经在代码中定义了一个 Calendar
对象,只需使用它即可:
Calendar calendar = Calendar.getInstance();
//..
int today = calendar.get(Calendar.DAY_OF_WEEK);
boolean isWeekend = (today == Calendar.SUNDAY) || (today == Calendar.SATURDAY);
并相应地更新您的代码:
//...
int today = calendar.get(Calendar.DAY_OF_WEEK);
boolean isWeekend = (today == Calendar.SUNDAY) || (today == Calendar.SATURDAY);
if(isWeekend && noWeekends == true) {
//Alarm is not wanted on the weekend
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//...
关于android - 防止周末重复报警,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33137835/
有人可以帮我解决这个问题吗?当填写周五、周六或周日时,需要返回"is",而在所有其他情况下,则需要返回“否”。每次它都以“JA”结尾...... 代码: // Nieuwe Array aan
我知道,标题并不具体,但我对 DatePicker 有疑问。我尝试禁用国庆节、周末和今天之后的 48 小时。目前我这样做了: 隐藏今天 + 48 小时,我使用 minDate: 3 隐藏周末 隐藏国庆
我必须计算以下两个日期之间的周末计数和假期计数。 var startDate = new Date("01/02/2014"); var endDate = new Date("02/
我正在尝试编写一个脚本来实现以下目的: 我需要根据一天中的时间和星期几将我页面上某个元素的颜色从绿色更改为红色。因此,例如,在星期六我需要一个不同于星期一的时间窗口。 我正在努力寻找有用的东西。我找到
给定 postgres 中的 2 个时间戳,你如何在不计算整个星期六和星期日的情况下计算时差? 或 如何计算给定时间间隔内的周六和周日的数量? 最佳答案 以下函数返回两个日期之间的完整周末天数。由于您
我无法在没有周末的情况下呈现日期时间图表。怎么做到的? 这是示例代码 ( http://jsfiddle.net/LLExL/1720/ ): $('#container').highcharts({
ax.plot_date((dates, dates), (highs, lows), '-') 我目前正在使用此命令通过 Matplotlib 绘制财务高点和低点.效果很好,但如何删除 x 轴上因没
从今天早上开始,我无法使用原始 HTTP 协议(protocol)在用户对象上设置 orgUnitPath。 PATCH /admin/directory/v1/users/testuser_1440
我有这些 Dag DAG_A(每天运行)、DAG_B(周一至周五运行)和 DAG_C(周六和周日运行),其中 DAG_A 依赖于 DAG_B 和 DAG_C。我尝试使用 External Task S
我正在使用 JQuery UI Datepicker Disbale Next Day After 12pm 中的代码禁用周末、公共(public)假期和第二天(如果在上午 10 点之后选择),但我不
我是一名优秀的程序员,十分优秀!