I want to send a notification when the time is between May 8 and May 12 just once every year.
But i stuck in compare with different year , it cause that will send notification every time when user start the application or reboot that if time is between May 8 and May 12.
It's confuse me with a lots of hours , some one can teach me how to fix the issue , that would be appreciated.
Thanks in advance.
My manifests:
<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>
My main activity will save the boolean
and 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();
}
My PlayReceiver:
this line of code if ( neverSend || !(firstYear == nextYear)){
confused me
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();
}
}
}
My 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);
}
}