Android AlarmManager & Notification - android

I need the AlarmManager to push a notification at the same time everyday. When I first loaded the app, it worked as I expected. Unfortunately after I rebooted my phone, there's no notification at the time. If I open the app, the notification does show. What I need is to remind the user to use the app when they don't. If they have to enter the app for it to work, it's worthless.
Here is some of my code:
AlarmManager am = getAlarmManager(ctx);
Intent i = new Intent(ctx, UpdateReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, 0, i, 0);
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(System.currentTimeMillis()));
//If current time is later than 22, the alarm time should set 22 in the next day
if (cal.get(Calendar.HOUR_OF_DAY)>22)
{
cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH)+1);
}
cal.set(Calendar.HOUR_OF_DAY, 22);
cal.set(Calendar.MINUTE, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP ,cal.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);
How can I get the app to always send the notifications correctly?

Unfortunately when you restart the device all of the AlarmManager stuff will be cleared.
...will be cleared if it is turned off and rebooted.
https://developer.android.com/reference/android/app/AlarmManager.html
You have to register a BroadcastReceiver for the boot like this:
<receiver android:name="com.your.app.BootCompletedReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
From your receiver you have to schedule your notification again (just like you do in your post).

Obviously, I think my code is right. so I moved the app to AVD, and it could work. My test phone is not original Android OS, I guess the ROM has changed the way AlarmManager works.

Related

AlarmManager not calling background services

I am trying to set two alarms which will run two different background services. I set the alarm inside the onCreate method of my activity class. But the problem is that the service classes which are extending IntentService are not getting called, i.e. their method onHandleIntent() is not getting called. This is how I set my alarms
//Creating alarm for showing notifications.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
//create an alarm for today if there is still time else schedule alarm for tomorrow.(Handled inside the one time alarm class).
//FIRST ALARM..............
Intent intent = new Intent(ActionBarTabActivity.this, ScheduleOneTimeAlarmForToday.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
//SECOND ALARM.............
Intent i = new Intent(ActionBarTabActivity.this,RemoteNotificationService.class);
PendingIntent pi = PendingIntent.getService(getApplicationContext(), 111, i, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), 1000 * 60, pi);
I have declared the services inside the minifest file as below
<service android:name="com.nss.common.services.ScheduleOneTimeAlarmForToday" />
<service android:name="com.nss.common.services.RemoteNotificationService" />
Also the alarms run properly on my old samsung phone but when I test it on my new Asus Zenfone or any other new phone, it doesn't show up.
Edit:
My logcat shows this:
10-19 12:25:05.605 634-744/? V/AlarmManager﹕ triggered: flg=0x10000000 cmp=com.nss.zobbers/com.nss.common.services.ScheduleOneTimeAlarmForToday Pkg: com.nss.zobbers
10-19 12:25:06.846 634-744/? V/AlarmManager﹕ triggered: cmp=com.nss.zobbers/com.nss.common.services.RemoteNotificationService Pkg: com.nss.zobbers
So I don't get it, my alarm is triggered but the service it needs to call doesn't get called? I have tried many posts but couldn't find the error. Please help, thanks in advance.
I made it to work, but I won't accept it as my answer as maybe someone will tell the exact reason why it doesn't work. Well instead of directly calling services via alarmManager, I modified my code to call a broadcast receiver which then calls the respective service. Now it seems to work perfectly on different devices.

Android Re-create service at given intervals using AlarmManager

I have read about this AlarmManager, but it's still a bit confusing to me.
So I have a Service in my app, which I want to run all the time (its a medicinal app, so its supposed to notify the user the whole time its being used), but even though its a Service, Android kills it from time to time, so I want to schedule it to be recreated in like 30mins interval, and then scheduled again. How can I do it?
For started services, there are two additional major modes of operation they can decide to run in, depending on the value they return from onStartCommand(): START_STICKY is used for services that are explicitly started and stopped as needed, while START_NOT_STICKY or START_REDELIVER_INTENT are used for services that should only remain running while processing any commands sent to them. See the linked documentation for more detail on the semantics.
http://developer.android.com/reference/android/app/Service.html
Study this link... for implementing HOW TO CREATE ALARM MANAGER
CREATE ALARM MANAGER CLASS
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// Set the alarm to start at 8:30 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);
// setRepeating() lets you specify a precise custom interval--in this case,
//20 minutes.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, alarmIntent);
ADD THIS TO MANIFEST
<receiver android:name=".SampleBootReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
Hope this will help you.

Calendar instance in android for AlarmManager

I am developing an app which displays notifications by using AlarmManager.
For that I'm taking the user input values for hour, minute and second.
Something like:
int hour = 4;
int min = 40;
int sec =36
Calendar Calendar_Object = Calendar.getInstance();
Calendar_Object.set(Calendar.HOUR, hour);
Calendar_Object.set(Calendar.MINUTE, min);
Calendar_Object.set(Calendar.SECOND, sec);
Intent myIntent = new Intent(MyView.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MyView.this,0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, Calendar_Object.getTimeInMillis(), myIntent);
Notifications and the rest of the code work fine, but the problem is that instead of 4:40:36 the notifications get invoked on the current time(as soon as i run/debug the app).
I think there is some problem in Calender_Object part.
Looking for a solution.
Thanks in advance.
Please note: Alarms will be executed immediately, if the notification time has elapsed already.
As a workaround you might want to consider a date part too. Or just a variable in memory which acts as a boolean if the time has elapsed or not.
From the Docs:
If the stated trigger time is in the past, the alarm will be triggered immediately. If there is already an alarm for this Intent scheduled (with the equality of two intents being defined by filterEquals(Intent)), then it will be removed and replaced by this one.
Also please consider the API level 19 version of how AlarmManager works.

Reg : Make sure the Alarm is persistent after we force kill the application

I have application which will depends on alarm , I have created an alarm it runs perfectly at same time every day.
ISSUE: If the my application is killed the alarm goes off.
I need solution to keep the alarm still persist even my application is closed.
I am asking because my application is very simple UI interface which will set alarm at particular time and at that time it does specified task that all
but FIRST how can i make alarm persist , similar to OUR android default ALARM apps ..
After setting the alarm in the android , even after we kill the default ALARM apps still we can see small ALARM icon on the top notifications and the alarm will trigger next day.
Can we do it for our own application.
Intent intent = new Intent(this, MusicPlayActivity.class);
intent.putExtra(EXTRA_MESSAGE, selectedFile.getPath());
pendingIndent = PendingIntent.getBroadcast(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Calendar firingCal = Calendar.getInstance();
Calendar currentCal = Calendar.getInstance();
// adding the hour and minutes to the calendar
firingCal.set(Calendar.HOUR_OF_DAY, hourSelected);
firingCal.set(Calendar.MINUTE, minutesSelected);
firingCal.set(Calendar.SECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
firingCal.getTimeInMillis(), 24 * 60 * 60 * 1000,
pendingIndent);

make an android app autostart

Is it possible to make an installed app auto start itself?
thanks
Read this article if you want to start an application on boot up.
Yes .
For this you have to use Alarm Notification .
You can found many tutorial for this on Net. Just google it..
You will found code something like this...
Intent intent = new Intent(FirstScreen.this, MyBroadcastListener.class);
PendingIntent sender = PendingIntent.getBroadcast(FirstScreen.this,
0, intent, 0);
// We want the alarm to go off 30 seconds from now.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 30);
// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
The BroadCastListener You will set can call any activity in return. of own app too..:)
Auto Start on any event? Check the IntentFilter out to make your app default for particular actions.
Auto Start on/after a particular time? check Alarm out!

Categories

Resources