My AlarmManager triggers every time service is run, not at set time - android

I am trying to create a SMS-app which sends out SMS at one set time of day. For this I have created two services SendMessagePeriodicService and SendMessageService.
This is my SendMesagePeriodicService, which is supposed to trigger SendMessageService at a set time of day. The problem is that it triggers every time the onStartCommand-method is called...
public class SendMessagePeriodicService extends Service {
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) { //FIXME
Calendar cal = Calendar.getInstance();
Intent i = new Intent(this, SendMessageService.class);
PendingIntent pintent = PendingIntent.getService(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
cal.set(Calendar.MINUTE, PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getInt("minute", -1));
cal.set(Calendar.HOUR_OF_DAY, PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getInt("hour", -1));
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pintent);
return Service.START_NOT_STICKY;
}
}
I have also checked that the values in the sharedPreferences are correct...
Does anyone have any idea why this is happening?

I would do that by setting the alarmmanager in the activity and than use BroadcastReceiver to send the sms when the alarm fires.
I use the same technique to start a notification on a specific time.
I think I have read somewhere the you use services nowadays only for fileoperations. But I´m not 100%.

It seems that the reason this happens was that the time I set for trigger time was in the past. This means that AlarmManager triggers immediately and not the next time this set time occurs.

Related

Set alarm in service and receive callback in the same service

My ReminderService uses the AlarmManager to inform the user about an upcoming event at a specific time. Is it possible that the AlarmManager informs the same service (ReminderService) or do I need to start another service to catch the pending intent? Until now this mechanism looks like this
public class ReminderService extends Service {
// ...
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// ..
/* When the alarm goes off the NotifyService will be started. Is it possible to inform **this**
service (ReminderService) and to handle the alarm? */
Intent alarmIntent = new Intent(this, NotifyService.class);
alarmIntent.putExtra(TodoTask.PARCELABLE_KEY, task);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, alarmIntent, 0);
Calendar calendar = Calendar.getInstance();
Date date = new Date(reminderTimeStamp*1000);
calendar.setTime(date);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
}
yes u can. You just need to change this line:
Intent alarmIntent = new Intent(this, NotifyService.class);
to this:
Intent alarmIntent = new Intent(this, ReminderService.class);
that way the ReminderService will receive the intent with TodoTask.PARCELABLE_KEY inside this same method public int onStartCommand(Intent intent, int flags, int startId)
Remember that there no guarantee that this will be the same instance of the service. If the service you're running get killed by the system a new instance of it will be started to handle the intent.
Also remember all the sleep and doze mode stuff mentioned by Larry_Schiefer on his answer.
You can use the same Service (or other component) to receive an alarm. However, not that your alarm will not be guaranteed to be delivered on schedule when in low power. Use a WakefulReceiver or your own combination of BroadcastReceiver and a wakelock to get your Service going. See this article for more details on power state and alarms.
Also, note that starting with Android Marshmallow your wakelocks will be ignored if the device is in Doze mode. There are other things you'll have to do to wake the device at a certain time when in Doze.

Android AlarmManager send Broadcast not on time

I use AlarmManager to set two action on time,
First action set on 10:00:00, and second action set on 10:15:00.
I can get two action broadcast,
and get first action broadcast on 10:00:03 (is OK),
but get second action broadcast on 10:29:15,14 minutes late!
How let AlarmManager can send broadcast on time ??
Set AlarmManager code:
#Override
public void onCreate() {
setSchedule();
}
private void setSchedule(){
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar offCal = Calendar.getInstance();
offCal.set(Calendar.HOUR_OF_DAY, 10);
offCal.set(Calendar.MINUTE, 15);
offCal.set(Calendar.SECOND, 00);
Intent offIntent = new Intent(this, AlarmReceiver.class);
offIntent.setAction(AlarmReceiver.ALUM_SCREEN_OFF);
PendingIntent offPending = PendingIntent.getBroadcast(this, 1,
offIntent, PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC, offCal.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, offPending);
Intent onIntent = new Intent(this, AlarmReceiver.class);
onIntent.setAction(AlarmReceiver.ALUM_SCREEN_ON);
Calendar onCal = Calendar.getInstance();
onCal.set(Calendar.HOUR_OF_DAY, 10);
onCal.set(Calendar.MINUTE, 00);
onCal.set(Calendar.SECOND, 00);
PendingIntent onPending = PendingIntent.getBroadcast(this, 2,
onIntent, PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, onCal.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, onPending);
}
Receiver code:
public class AlarmReceiver extends BroadcastReceiver {
public static final String ALUM_SCREEN_ON = "screenOn";
public static final String ALUM_SCREEN_OFF = "screenOff";
private static final String TAG = "AlarmReceiver";
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.d(TAG, "get braodcast action:"+intent.getAction());
}
Hi you are using setRepeating method which wont give alarm at exact time.Alarm manager will strongly discourage other apps apart from system apps to trigger alarm accurately for the better battery optimization.so if u want to trigger alarm at a particular time means you can use setExact() method This also wont give you complete gaurantee but it will give you the most accuracy one .While setting multiple alarms at a single time make sure you are giving different ids in `
PendingIntent onPending = PendingIntent.getBroadcast(this, 2,
onIntent, PendingIntent.FLAG_UPDATE_CURRENT);
here you gave 2 as id and it should be different for different alarms.
Since KitKat the set and setRepeating APIs are not exact. If you absolutely need the alarm to run at a specific time, use setExact. Consider using setWindow instead if the alarm time is not critical.
There is no setExactRepeating, so if you need that, you need to use setExact and them re-arm your alarm. But unless your are doing time critical stuff, that's not recommended.

Starting service with Alarm Manager on Android?

Hi a read other question retated to this but the responses never resolve my issue.
I want to start a service after a specific time with an Alarm Manager
alarmMgr = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmServiceIntent=new Intent(ListActivity.this, AlarmLevelServiceImproved.class);
alarmServiceIntent.putExtra("username",usernameID);
alarmServiceIntent.putExtra("password",userPass);
alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, alarmServiceIntent, 0);
. . .
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND,10);
//calendar.set(Calendar.HOUR_OF_DAY, 19);
//alarmMgr.setInexactRepeating(AlarmManager.RTC,calendar.getTimeInMillis(),
// AlarmManager.INTERVAL_HALF_HOUR,alarmIntent);
//alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
// 1000 * 10, alarmIntent);
alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
Log.d("MioLog", "Alarm setted");
My service is:
public class AlarmLevelServiceImproved extends Service {
#Override
public void onCreate() {
Log.d("MioLog","MyAlarmService.onCreate()");
Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show();
}
#Override
public IBinder onBind(Intent intent) {
Log.d("MioLog","MyAlarmService.onBind()");
Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show();
return null;
}
#Override
public void onDestroy() {
super.onDestroy();
Log.d("MioLog","MyAlarmService.onDestroy()");
Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("MioLog","MyAlarmService.onStart()");
Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
return super.onStartCommand(intent, flags, startId);
}
}
And I add this on AndroidManifest.xml (The service class is on the sub-package "services"):
<service
android:name=".services.AlarmLevelServiceImproved"
android:enabled="true" />
But the service don't start and no log is printed.
Any idea how to start a Service with the AlarmManager?
I also suived this tutorial but I don't find any difference to my code
Thanks
To start a service with a PendingIntent, you need to use the static method PendingIntent.getService() to retrieve the appropriate PendingIntent.
Your code seems to be using PendingIntet.getBroadcast() for whatever reason. If you intend to have the AlarmManager indeed send a broadcast, you can start your service in your BroadcastReceiver, else change that line to getService():
from
alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, alarmServiceIntent, 0);
to
alarmIntent = PendingIntent.getService(getApplicationContext(), 0, alarmServiceIntent, 0);
You are using getBroadcast() to create the PendingIntent, but the Intent points to a service. That combination will not work.
Since you are using RTC_WAKEUP, you need to use getBroadcast() and a WakeLock to get reliable results. One approach is to use WakefulBroadcastReceiver as the base class of your receiver, where you then forward the work onto an IntentService.
Also, please do not pass user names and passwords in extras, as those are visible to anything that gets its hands on the Intent or PendingIntent. In this case, that should just be your app and the core OS, but I would still aim to do something a bit more secure.

How to prevent AlarmManager from being recreated with Server?

I have this Service that do other stuff in the background and must always be running. On its onCreate i've set an Alarm that sends an email once a day. The server is not running in foreground, so sometimes its get killed by the system, and then created again, and when that happens its creates the alarm again, and sends another email every time it happens. So my question is, is there a way to prevent it from triggering the alarm if its already set for repeating in the system? Or is there another "place" in the Service where i can create the alarm once and prevent it for being recreated with the it?
public class ObserverService extends Service {
private String BABAS = "babas";
#Override
public void onCreate() {
setRecurringAlarm(this);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
private void setRecurringAlarm(Context context) {
Log.i(BABAS, "entrou no set alarme");
Calendar updateTime = Calendar.getInstance();
updateTime.setTimeZone(TimeZone.getDefault());//getTimeZone("GMT"));
updateTime.set(Calendar.HOUR_OF_DAY, 00);
updateTime.set(Calendar.MINUTE, 00);
Intent downloader = new Intent("ALARME");//context, AlarmReceiver.class);
PendingIntent recurringDownload = PendingIntent.getBroadcast(context,
0, downloader, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, recurringDownload);
}
}
If that code is everything your Service does you can completely get rid of the Service.
The only thing the service does is schedule a Broadcast once a day on the alarm manager, why not do this schedule directly from the Activity?
edit:
Given your comment, my original answer remains:
The Alarm set on the AlarmManager is permanent until the device is turned off or rebooted.
So you can set the first alarm via Activity and mark it as active on the SharedPreferences. Create a BroadcastReceiver to receive onBoot events, this receiver will check the SharedPreferences for if the alarm is active and in case it is, re-register it on the AlarmManager.
Your service still does the same ContentObserver stuff, but have no association with the recurring AlarmManager event.
I reckon that would be cleanest solution.
Alternatively (but I don't think it's a good/clean solution), you can use the AlarmManager.cancel() to remove the existing PendinIntent, and then re-trigger it
http://developer.android.com/reference/android/app/AlarmManager.html#cancel(android.app.PendingIntent)

How to implement yearly and monthly repeating alarms?

I want to set monthly and yearly alarm in my app.I did this for weekly. AlarmManager.INTERVAL_DAY helped me for that.But i could not find good way to implement monthly and yearly repeat.
Searched so far:
http://www.satyakomatineni.com/akc/display?url=displaynoteimpurl&ownerUserId=satya&reportId=3503
http://groups.google.com/group/android-developers/browse_thread/thread/9f946e40307073c4?pli=1
Is any other way available to do this? Any help appreciated.
I think that, you have two inherent issues with this approach:
AlarmManager will not accept large time intervals because the number of millis will overflow the argument
I do not think Alarms will survive reboots of your phone that will most certainly happen during such a long period of time.
I advice that you store each alarm in a safe place and use a combination of AlarmManager and onBoot receivers to check if one of the alarms from your list must be fired this very day and just reschedule an alarm to wake you up tomorrow if none does.
public class AlarmService extends Service {
//compat to support older devices
#Override
public void onStart(Intent intent, int startId) {
onStartCommand(intent, 0, startId);
}
#Override
public int onStartCommand (Intent intent, int flags, int startId){
//your method to check if an alarm must be fired today
checkForTodayAlarmsAndBehaveAppropriately();
//reschedule me to check again tomorrow
Intent serviceIntent = new Intent(AlarmService.this,AlarmService.class);
PendingIntent restartServiceIntent = PendingIntent.getService(AlarmService.this, 0, serviceIntent,0);
AlarmManager alarms = (AlarmManager)getSystemService(ALARM_SERVICE);
// cancel previous alarm
alarms.cancel(restartServiceIntent);
// schedule alarm for today + 1 day
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
// schedule the alarm
alarms.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), restartServiceIntent);
}
}
To start your service at boot time use this :
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class serviceAutoLauncher extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(context,AlarmService.class);
context.startService(serviceIntent);
}
}
Finally add this to your manifest to schedule your serviceAutoLauncher to be launched at each boot:
<receiver android:name="serviceAutoLauncher">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<category android:name="android.intent.category.HOME"></category>
</intent-filter>
</receiver>
I solved the issue. In my app,multipal alarms were set with different repeating time intervals.So in my broadcast receiver for alarm,i re-scheduled the alarm based on for what time it was scheduled to be repeated.I used calendar object to add month and year accordinly and again set it for next alarm.i used code like this(for scheduling it for next month)-
PendingIntent sender = PendingIntent.getBroadcast(context,Integer.parseInt(Long.toString(id)), intent1, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(calendar.getTimeInMillis());
calendar.add(Calendar.SECOND, 30);
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
calendar.add(Calendar.MONTH, 1);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
To add to Laurent's response. To abstract from calculating recurrence manually I suggest to take a look at this RFC-2445 implementation. It works fine on Android and can save you a lot of pain.

Categories

Resources