Android widget refresh not working - android

My widget should refresh its textviews every day at 0:00. In the widget_provider.xml I set android:updatePeriodMillis="1000" but I read that the minimum update period is 30 minutes and I have to use alarmManager for this. So i want an alarm that triggers the refresh every day at 0:00. UpdateService.class handles the refreshing (setting texts for textviews based on date. The class is just not called until around half an hour after midnight)
In the public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) method I am using this code:
Intent intentN = new Intent(context, UpdateService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentN, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendarN = Calendar.getInstance();
calendarN.setTimeInMillis(System.currentTimeMillis());
calendarN.add(Calendar.HOUR_OF_DAY, 20);
calendarN.add(Calendar.MINUTE, 44);
calendarN.add(Calendar.SECOND, 5);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendarN.getTimeInMillis(), 7000, pendingIntent);
(I am trying with actual time now)
Is this the right place for this code? Why doesn't it work? I am using toast messages in UpdateService.java, but i never get the toast in the onStart(Intent intent, int startId) method apart from the first time, when the app in installed.
Thanks in advance
Update
With the help of probablykevin, i rewrite the code. I tried this code out in an Activity and it works:
Intent myIntent = new Intent(MainActivity.this, UpdateService.class);
PendingIntent pendingIntent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 8000, pendingIntent);
Toast.makeText(MainActivity.this, "Start Alarm", Toast.LENGTH_LONG).show();
However if I modify it for the AppWidgetProvider class (and put it there ofc) it does not work. I never even get the toast msg:
Intent myIntent = new Intent(context, UpdateService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTimeInMillis(System.currentTimeMillis());
calendar2.add(Calendar.SECOND, 10);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar2.getTimeInMillis(), 8000, pendingIntent);
Toast.makeText(context, "Start Alarm", Toast.LENGTH_LONG).show();

First, your repeating time should be 24 * 60 * 60 * 1000, as it is in milliseconds.
Second your start time could be found using the time left until the next 0:00:
final long MILLIS_IN_DAY = 24 * 60 * 60 * 1000;
long timeUntilMidnight = MILLIS_IN_DAY - (System.currentTimeMillis() % MILLIS_IN_DAY);
Then set your alarm by adding the time left to the current time:
alarmManager.setRepeating(...System.currentTimeMillis() + timeUntilMidnight, MILLIS_IN_DAY...);
Make sure you don't set multiple of the same alarm. I like to call
alarmManager.cancel(pendingIntent);
before setting it again just to be sure.

Related

Prevent notification when launching App

I have a AlarmManager in order to show notification. I don't want that I get a notification everytime I launch the app. I read a lot but nothing was satisfying. My method doNotification in MainActivity.java looks like this:
public void doNotification(){
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(this, AlarmReceiver.class);
PendingIntent pending = PendingIntent.getBroadcast(this, 0, i,PendingIntent.FLAG_CANCEL_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP ,calendar.getTimeInMillis(), 1000*60*60*24*30 , pending); //every month
}
This method is in the OnCreate of my MainActivity class.
I know, that this is the reason why I get a notification everytime I launch my app.
My goal was to fire a notification every month, starting from the Date where the app was launched the first time. E.g User launches app on 5. July then you will get a notification on every 5th of every month.
How can I achieve this?
Thank you.
EDIT: According to the comments I used SharedPreferences is that a right use of it. Here is the new code:
public void doNotification(){
boolean isFirstAlarm = getSharedPreferences("ALARMFIRSTTIME",MODE_PRIVATE).getBoolean("isFirstAlarm",false);
if(!isFirstAlarm) {
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(this, AlarmReceiver.class);
PendingIntent pending = PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 60 * 24 * 30, pending); //every month
getSharedPreferences("ALARMFIRSTTIME", MODE_PRIVATE)
.edit()
.putBoolean("isFirstAlarm",true)
.apply();
}
}

Android set alarmManager to trigger daily

In my app, I need to start a service at 2:00pm daily. Right now I wrote the code to trigger the alarm once, this code is ran every time I open the app:
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, DownloadReceiver.class);
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr.cancel(pIntent);
Calendar cal= Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY,refreshhour);
cal.set(Calendar.MINUTE,refreshmin);
cal.set(Calendar.SECOND, 0);
// if the scheduler date is passed, move scheduler time to tomorrow
if (System.currentTimeMillis() > cal.getTimeInMillis()) {
cal.add(Calendar.DAY_OF_YEAR, 1);
}
if(android.os.Build.VERSION.SDK_INT>=23) {
alarmMgr.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), pIntent);
}
else{
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pIntent);
}
Q1. I used setAndAllowWhileIdle() for sdk above 23 in case the device is in Doze mode. I cannot find any option in this function that I can set the alarm to repeat every day.
Q2. I also have questions about setInexactRepeating() , normally it is set to repeat every day by setting the parameter INTERVAL_DAY , but in the docs, it says
As of API 19, all repeating alarms will be inexact and subject to
batching with other alarms regardless of their stated repeat interval.
Does this mean INTERVAL_DAY does not work anymore, so how can I set the alarm daily without rerunning this function and reset alarmManager?
Try below code will solve your problem-
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
boolean flag = (PendingIntent.getBroadcast(this, 0,
new Intent("totime.action.string"),
PendingIntent.FLAG_NO_CREATE) != null);
if(!flag)
{
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 14);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Intent intent = new Intent("totime.action.string");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) Data_Graph.this.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),(24*60*60*1000), pendingIntent);
}

Repeating Alarm not working

I know this type of questions are aksed so many times...but please first read my question before down voting or marking as duplicate.
I have referred so many SO questions like this for managing alarm but cant find any solution so i am here.
In my app i want to trigger repeating alarm one at daily 8.00 am and others as user specified.
First i tired to repeat alarm using setRepeating...this is my code
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.AM);
//calendar.set(Calendar.MILLISECOND, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent pintent = new Intent(this, AlarmReceiver.class);
pintent.putExtra("id", 0);
pintent.putExtra("ontime", false);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
pintent, PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
Actually the problem is alarm is not firing after long sleep. If it test using changing date and time it fires perfactly....but it stops firing after a night...
So i switched to second option as mentioned in given link. I set alarm using setExact as follow and reschedule it on each fire.
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.AM);
//calendar.set(Calendar.MILLISECOND, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent pintent = new Intent(this, AlarmReceiver.class);
pintent.putExtra("id", 0);
pintent.putExtra("ontime", false);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
pintent, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(calendar.getTimeInMillis(), pendingIntent);
am.setAlarmClock(alarmClockInfo, pendingIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
am.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
} else
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
But steel i am having same issue. It is not repeating daily. Please also consider this is only the example of my default alarm. There can be N no of exact and repeating alarms as user specified.
Someone also suggest me to use service rather than alarm manager, but my service might be killed so i am afraid to use that. One other solution is to check due date and set all alarm daily, but i dont think this is the right way. Is it?
Please suggest me any other method or help me to improve my code. I badly need this to work. Thanx all. My testing devices are HTC One X (4.3), Redmi Prime(6.0) and Mi4i (5.0.2)
I had many problems setting repeating alarms, but in the end this code was working on all my testing devices, maybe this helps:
// set time
Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
c.set(Calendar.HOUR_OF_DAY, 8);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
long startUpTime = c.getTimeInMillis();
// startupTime + 24 hours if alarm is in past
if (System.currentTimeMillis() > startUpTime) {
startUpTime = startUpTime + 24 * 60 * 60 * 1000;
}
// initialize alarm
AlarmIntent alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, 0);
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, startUpTime, 24 * 60 * 60 * 1000, pendingIntent);

Set monthly repeating notification in Android

I would like to set a notification at 8th of every month.
This is what i did:
Intent myIntent = new Intent(remember.this, receiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(remember.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
// Set the alarm to start at approximately 8th of
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.DAY_OF_MONTH, 8);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY*30, pendingIntent);
However what i got was a constant notification within minutes. And this does not go away.
Would appreciate your help here as i really do not know where went wrong.
Set calendar to upcoming 8th of the month.
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis() , pendingIntent);
Inside receiver.class
public void onReceive(Context context, Intent intent) {
//do action needed
//invoke alarmManager in mainActivity
// cancel the previous alarm
// set the new alarm for the next month
}

Cancelling alarm from other class that the one which initiated it

I set alarm from one class using this code
Intent myIntent = new Intent(ClassOne.this, AlarmService.class);
pendingIntent = PendingIntent.getService(ClassOne.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(currentDate.getTime());
long when = calendar.getTimeInMillis();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when, (7 * 24 * 60 * 60) * 1000, pendingIntent);
Now I need to cancel the pending alarms from another class. Can I just use this code?
Intent myIntent = new Intent(ClassTwo.this, AlarmService.class);
PendingIntent pendingIntent = PendingIntent.getService(ClassTwo.this, 0, myIntent, 0);
AlarmManager alarmManagerCancel = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManagerCancel.cancel(pendingIntent);
Or there is a better/proper way to cancel pending alarms?
As long as the PendingIntent are equivalent as the one you used to register the alarm, it doesn't matter where you call cancel() from. Two PendingIntents are equivalent (equals() returns true) if the underlying intents and request codes are the equivalent.

Categories

Resources