I want to display a notification every three hours from 10am to 22pm, but my application also displaying a notification whenever users open their phone. In order to solve this every time the notification process is triggered I set the current time plus 3 hours in my Shared Preference. I use this time value to trigger my repeating Alarm but it does not seems to work correctly.
How can I display notification only at the scheduled time of the repeating alarm(every three hours) and stop sending notification whenever someone reboot its phone?
My code is the following:
A Broadcast Receiver which is trggered wheneven users reboot their phone is calling the following AlarmService :
public class AlarmService extends Service {
private static final String TAG = "MyService";
@Override
public void onStart(Intent intent, int startid) {
Calendar calendar ;
int sp= SharedPrefs.getDefaults("TIME", this);
Intent i = new Intent(this, NotificationBarAlarm.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, sp);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),3*60*60*1000 , pi);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
Toast.makeText(this, "My Service stopped", Toast.LENGTH_LONG).show();
Log.d("TAG", "onDestroy");
}
}
which is sending an Intent to the following Broadcast Receiver:
public class NotificationBarAlarm extends BroadcastReceiver {
NotificationManager notifyManager;
@Override
public void onReceive(Context context, Intent intent) {
Log.d("NotificationAlarm", "onReceive");
Time time = new Time();
long currenttimeMilliseconds = System.currentTimeMillis();
time.set(currenttimeMilliseconds);
int t = time.hour;
long updatedTime= currenttimeMilliseconds + 10800000;
Time nextalarmmill = new Time();
nextalarmmill.set(updatedTime);
int nextalarm = nextalarmmill.hour;
SharedPrefs.setDefaults("TIME", nextalarm, context);
Log.d("UPDATE TIME", "The next alarm is set at"+" "+ nextalarm);
notifyManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
if (t >= 10 && t <= 22) {
// This Activity will be started when the user clicks the notification
// in the notification bar
Intent notificationIntent = new Intent(context,
AlarmReceiverActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
Notification notif = new Notification(R.drawable.ic_launcher,
"A new notification just popped in!",
System.currentTimeMillis());
// sound when the notification shows up.
Uri alarmSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notif.sound = alarmSound;
notif.setLatestEventInfo(context, "Questionnaire",
"Please fill it up", contentIntent);
notifyManager.notify(1, notif);
}
}
}
Here is the code of the Shared Preferences:
public class SharedPrefs {
public static void setDefaults(String key, int value, Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(key, value);
editor.commit();
}
public static int getDefaults(String key, Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getInt(key, 10);
}