Reputation: 107
I am building an app which triggers a notification at 11:30 IST..I have Tested It Its Working But After 11:30 Whenever i open my app it issues a notification...
i don't know why i think it should be some error in condition in time checking...
i want notification to be triggered on time only once in a day...and then the next day...:)
please resolve this thanks in advance
**MainActivity.java**
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (ProgressBar)findViewById(R.id.progressBar);
spinner.setVisibility(View.GONE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 11);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 00);
Intent intent1 = new Intent(MainActivity.this, AlarmReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
**AlarmReciever.java**
package com.alivestats.alivestats;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
public class AlarmReciever extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int MID = 1;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context).setSmallIcon(R.drawable.background)
.setContentTitle("title")
.setContentText("description").setSound(alarmSound)
.setAutoCancel(true).setWhen(when)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
notificationManager.notify(MID, mNotifyBuilder.build());
MID++;
}
}
Upvotes: 3
Views: 2549
Reputation: 2345
I also had this problem & tried every suggestions on this page though, I couldn't fix my problem. Here is the proposed solution for this problem and it works for me.
Register the receiver in manifest.xml
<receiver android:name=".receivers.DayAlarmReceiver"/>
Set the alarm manager
public void setDayAlarm(Context context) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 4);
AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent alarmIntent = new Intent(context, DayAlarmReceiver.class);
alarmIntent.putExtra("HOUR_OF_DAY", 4);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 5555, alarmIntent, 0);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent);
Log.d("Set the day alarm");
}
Check intent extras in onReceive() method of the receiver class
@Override
public void onReceive(Context context, Intent intent) {
int setHour = intent.getIntExtra("HOUR_OF_DAY", -1);
Calendar rightNow = Calendar.getInstance();
int currentHour = rightNow.get(Calendar.HOUR_OF_DAY);
if(currentHour == setHour){
Log.d("This is perfect, Trigger the alarm");
showNotification(context, msg);
}else{
Log.d("This is perfect, Trigger the alarm");
}
Upvotes: 0
Reputation: 26
To prevent an Alarm from continually triggering each time the App is launched (after the alarm time has passed), the following can be added:
Calendar timeNow = Calendar.getInstance();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 11);
calendar.set(Calendar.MINUTE, 30);
if(calendar.before(timeNow)){
calendar.add(Calendar.DATE, 1);
}
mAlarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, mAlarmPendingIntent);
I found the above example here. The time now is checked against the Alarm time and if the Alarm time is before the time now then another day is added to the alarm. Hopefully this helps :) .
Upvotes: 0
Reputation: 1
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR, 6);
int myPreferedTime=calendar.get(Calendar.HOUR);
Calendar rightNow = Calendar.getInstance();
int currentHour = rightNow.get(Calendar.HOUR_OF_DAY);
Log.i("current time","is"+currentHour);
int difference=myPreferedTime-currentHour;
Log.i("Differencevalue","is"+difference);
switch(difference)
{
case 1:
rightNow.add(Calendar.HOUR_OF_DAY, 1);
break;
case 2:
rightNow.add(Calendar.HOUR_OF_DAY, 2);
break;
case 3:
rightNow.add(Calendar.HOUR_OF_DAY, 3);
break;
case 4:
rightNow.add(Calendar.HOUR_OF_DAY, 4);
break;
case 5:
rightNow.add(Calendar.HOUR_OF_DAY, 5);
break;
case 6:
rightNow.add(Calendar.HOUR_OF_DAY, 0);
break;
case -1:
rightNow.add(Calendar.HOUR_OF_DAY,23);
break;
case -2:
rightNow.add(Calendar.HOUR_OF_DAY,22);
break;
case -3:
rightNow.add(Calendar.HOUR_OF_DAY,21);
break;
case -4:
rightNow.add(Calendar.HOUR_OF_DAY,20);
break;
case -5:
rightNow.add(Calendar.HOUR_OF_DAY,19);
break;
case -6:
rightNow.add(Calendar.HOUR_OF_DAY,18);
break;
case -7:
rightNow.add(Calendar.HOUR_OF_DAY,17);
break;
case -8:
rightNow.add(Calendar.HOUR_OF_DAY,16);
break;
case -9:
rightNow.add(Calendar.HOUR_OF_DAY,15);
break;
case -10:
rightNow.add(Calendar.HOUR_OF_DAY,14);
break;
case -11:
rightNow.add(Calendar.HOUR_OF_DAY,13);
break;
case -12:
rightNow.add(Calendar.HOUR_OF_DAY,12);
break;
case -13:
rightNow.add(Calendar.HOUR_OF_DAY,11);
break;
case -14:
rightNow.add(Calendar.HOUR_OF_DAY,10);
break;
case -15:
rightNow.add(Calendar.HOUR_OF_DAY,9);
break;
case -16:
Log.i("am here","bcoz time is 11 clock");
rightNow.add(Calendar.HOUR_OF_DAY, 8);
break;
case -17:
rightNow.add(Calendar.HOUR_OF_DAY,7);
break;
case -18:
rightNow.add(Calendar.HOUR_OF_DAY,6);
break;
default:
break;
}
Intent intent1 = new Intent(MainActivity.this, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent1, 0);
Log.i("am sending", "yes");
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Log.i("time holding with me", "calendar stores time like" + rightNow.getTimeInMillis());
am.setRepeating(AlarmManager.RTC_WAKEUP, rightNow.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
Upvotes: 0
Reputation: 107
After So Much Of Tea and Coffee i coded the answer myself...
long t = System.currentTimeMillis();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 11);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 00);
if (t <= calendar.getTimeInMillis()) {
//calendar.add(Calendar.DATE, 6); //add 6 days.
Intent intent1 = new Intent(MainActivity.this, AlarmReciever.class);
intent1.setAction("NOTE");
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
//am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
else{
}
i have just compared system time to the notification time..if notifcation time is less than equal to system time its true and reciever will work..but if not then nothing will happen...
BTW thanks for helping @tiny and @Mahesh
Upvotes: 4
Reputation: 6251
Intent intent1 = new Intent(MainActivity.this, AlarmReciever.class);
intent1 .setAction("ilove");
public void onReceive(Context context, Intent intent) {
if(!"ilove" .equalsIgnoreCase(getAction()))log.e("test",intent.getAction());
}
<receiver android:name="com.android.app.AlaramReceiver" android:exported="false">
<intent-filter>
<action android:name="ilove" />
</intent-filter>
</receiver>
Upvotes: 0