我想在5月8日到5月12日之间每年发一次通知。
但我坚持与不同的年份相比,它会导致每次用户启动应用程序时发送通知,或者如果时间是在5月8日到5月12日之间重新启动。
这让我困惑了很多时间,有些人可以教我如何解决这个问题,我们将不胜感激。
提前致谢。
我的清单:
<application>
<!-- -->
<receiver android:name=".AlarmSetting.PlayReceiver">
<intent-filter>
<action android:name="play_hskay" />
</intent-filter>
</receiver>
<!-- -->
<receiver android:name=".AlarmSetting.AlarmInitReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
我的主要活动将保存boolean
和year
:
//Setting
public void startAlarmSetting() {
Toast.makeText(this, "Starting setting", Toast.LENGTH_SHORT).show();
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
Intent intent = new Intent(this, PlayReceiver.class);
intent.putExtra("msg", "play_hskay");
PendingIntent pi = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);
long time = cal.getTimeInMillis();
//Save the time when user start application
SharedPreferences preferences = getSharedPreferences("time", MODE_PRIVATE);
preferences.edit().putLong("saveTime", time)
.putInt("saveFirstYear",2017)
.putInt("saveNextYear",year)
.putBoolean("saveBoolean",true)
.apply();
}
我的PlayReceiver:
这行代码if ( neverSend || !(firstYear == nextYear)){
让我困惑
public class PlayReceiver extends BroadcastReceiver {
private Context context;
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Starting playReceiver TDU", Toast.LENGTH_SHORT).show();
Bundle bData = intent.getExtras();
// load current time
Calendar current = Calendar.getInstance();
//set first time
Calendar firstLimit = Calendar.getInstance();
firstLimit.set(Calendar.MONTH, 4);
firstLimit.set(Calendar.DAY_OF_MONTH, 8);
firstLimit.set(Calendar.HOUR, 12);
firstLimit.set(Calendar.MINUTE, 30);
firstLimit.set(Calendar.SECOND, 30);
//set second time
Calendar secondLimit = Calendar.getInstance();
secondLimit.set(Calendar.MONTH, 4);
secondLimit.set(Calendar.DAY_OF_MONTH, 12);
secondLimit.set(Calendar.HOUR, 12);
secondLimit.set(Calendar.MINUTE, 30);
secondLimit.set(Calendar.SECOND, 30);
SharedPreferences preferences = context.getSharedPreferences("time", context.MODE_PRIVATE);
boolean neverSend = preferences.getBoolean("saveBoolean",false);
//int firstYear = preferences.getInt("saveFirstYear",0);
int firstYear = 2017;
int nextYear = preferences.getInt("saveNextYear",0);
//neverSend will be true when first time.
if ( neverSend || !(firstYear == nextYear)){
if (current.after(firstLimit) && current.before(secondLimit)) {
if (bData.get("msg").equals("play_hskay")) {
sendNotification();
//change the nextYear and save it.
Calendar calendar=Calendar.getInstance();
int sendYear = calendar.get(Calendar.YEAR);
preferences.edit().putInt("saveNextYear",sendYear).putBoolean("saveBoolean",false)
.apply();
}
} else {
Toast.makeText(context, "out of the period", Toast.LENGTH_LONG).show();
}
}
}
我的AlarmInitReceiver:
public class AlarmInitReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Start~~~~TDU", Toast.LENGTH_LONG).show();
SharedPreferences preferences = context.getSharedPreferences("time", context.MODE_PRIVATE);
long setTime = preferences.getLong("saveTime", 0);
Intent intentForSetTime = new Intent(context, PlayReceiver.class);
intentForSetTime.putExtra("msg", "play_hskay");
PendingIntent pi = PendingIntent.getBroadcast(context, 1, intentForSetTime, PendingIntent.FLAG_ONE_SHOT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, setTime, pi);
}
}
答案 0 :(得分:1)
可能这是因为每次用户启动应用程序时都会.putBoolean("saveBoolean",true)
。