Stopping alarms after the app is killed Android - android

I have a situation in my application. I am calling a service using an alarm. The alarm wakes up every 5 min and calls the service.
But it may so happen that the user might close my app and I am allowing the functionality of the service to work even if the user is not using my app.
To stop the service there are two ways either the user comes back to the app and presses a button which will cancel the alarm OR the second way is say after x time I want to stop the service i.e cancel the alarm from a broadcast receiver.
Now how can I do the second way ? When I tried to get reference of the AlarmManager it is giving me error of Null Pointer. (I am accessing this alarm manager from a broadcast receiver)
Can anyone give me suggestion on how to cancel repeating alarms from outside the activity context ?
Thanks :)

You can stop your service like this
context.stopService(new Intent(context, YourService.class))
Also in order to cancel the alarm you can do this
Intent intent = new Intent(this, YourClass.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
I hope this might help you

In your BroadcastReceiver try this:
public void onReceive(Context context, Intent intent) {
context.stopService(new Intent(context, YourService.class));
}

Related

Setting alarms daily android

I want to make my application set alarms every day at lets say 7 am a list of pills for the user. So far I have been doing it when a user adds a new pill, I will set the alarm directly, but I want to make it set alarms for today only. I am able to get a list of pills for some day using xpath, and getting the pills in a list. Now I was thinking if this is feasible to have some kind of hidden activity that keeps running or something that will set the daily pills. If someone could give me directions as to what I should be looking for to solve this problem, any kind of help would be appreciated.
You should use: Alarm Manager. And place it in Service. Read also about BroadcastReceiver
I will give an idea for this.
Schedule the first Alarm at 7 am using the set method of AlarmManager and register a BroadcastReceiver to be executed at 7 am using the same AlarmManager.
At 7 am your Alarm and BroadcastReceiver will execute. In the onReceive method of your BroadcastReceiver again set the Alarm and BroadcastReceiver so that it becomes a self loop.
pseudo code to set broadcastReceiver class:
Intent intent = new Intent(this, broadcastReceiver.class);
intent.putExtra("subject", subject);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
0, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager am= (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, "Your specific time", pendingIntent);
broadcastReceiver.class:
public class TimeAlarm extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//set the alarm and broadcast receiver again
}

Why is AlarmManager.RTC waking my device?

I have a very simple alarm manager to do things while the device is awake. So I figured AlarmManager.RTC was my best approach. As according to AlarmManager API the device is supposed to ignore the next occurring alarm until the device wakes up, then will it send its pending intent instructions.
Here is the code that I have scheduling the Alarm:
Intent intent = new Intent(getApplicationContext(), WorkerService.class);
PendingIntent pIntent = PendingIntent.getService(getApplicationContext(), 0, intent, 0);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), 10000, pIntent);
And here is the code inside the "WorkerService.class"
public void onCreate() {
super.onCreate();
stopSelf();
}
public void onDestroy() {
super.onDestroy();
Log.i("Service Status", "Service exited");
}
What am i doing wrong here?
Now I know I can write a screen receiver, but then I would have to have another service open to hold that receiver (kinda annoying) when alarm manager is supposed to be ignoring my alarm when the screen is off.
I used Log.i to inform me if the service ran. I was able to tell in eclipse's logcat what the output was by using ADB Over WiFi this way the device was allowed to sleep.
Thanks guys, im truly stumped.

Android - Alarm doesn`t fire sometimes after long period

I have a big problem with my app for several days now. I appologize if my english is not so native in advance. I implemented an AlarmManager to give the user of my app the option to start a certain service at any time of the current or the next day. So e.g. the user might choose to set the time for my service to tomorrow at 08:00 a.m. and then starts the service.
The alarm manager should now wait the calculated time from now till the chosen time (i calculated the time also manually and it is correct!) and then start the service. My problem now is that sometimes the alarmmanager is starting my service and somtimes not. It seems that if it has to wait for lets say more than 4 hours it is not working any more and my service is not called. I have set all neccessary permission otherwise it would not work at all. You can have a look at the code of the alarmmanager below:
someIntent = new Intent();
someIntent.setAction("START_SERVICE");
AlarmManager alarams ;
alarmIntent = PendingIntent.getBroadcast(MainActivity.this, 0, someIntent, PendingIntent.FLAG_CANCEL_CURRENT);
alarams = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarams.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+delay, alarmIntent);
The broadcast receiver is implemented like this (and it is registered!):
alarmReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(MainActivity.this, MyService.class);
startService(myIntent);
}
};
As I told you, if it is not working it stops before starting the service.
EDIT:
I have an idea. I read something in this thread: Android long running service with alarm manager and inner broadcast receiver
It might be, that my constructor for the intent "someIntent" only works for BroadcastReceivers declared in the manifest file. So in my case I should maybe use someIntent = new Intent("START_SERVICE") instead of someIntent = new Intent(); someIntent.setAction("START_SERVICE"). This effect is called tunneling - i will figure it out, if it works i will post my experience here. Thanks for the well explained answer on the mentioned thread! If you have any other ideas or the same experiences like me please let me know!
eMu
If the device is shutdown and start up then you will not get the alarm maanger broadcast receiver.
Implement OnBootReceiver which will receive the OnBoot completed and there you can start your pending alarms that were not fired.

Android - start alarm service immediately?

I have created an On Boot Receiver to repeatedly call a wakeful intent service every 5 minutes but cannot figure out how to start the service immediately when the app is installed..? I do not want to rely on the user rebooting their device before it starts to run!
Here is my code so far :
public class OnBootReceiver extends BroadcastReceiver {
private static final int PERIOD = 300000; // check every 5 minutes
#Override
public void onReceive(Context context, Intent intent) {
AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, OnAlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 60000, PERIOD, pi);
}}
Can anyone help me out pls? :)
If you want to set an alarmmanager to start your service when the app is installed, then it's not possible. It's a OS limitation, security if you will. But if you want to start the service in the moment the app starts, just call it, it will keep runing.
Essentially, since the Application object is created when the application is started and when the BOOT_COMPLETED Intent is received, you could register with the AlarmManager in the onCreate method in your custom Application class. Just be aware that the Application object is instantiated every time the process starts, which includes cases where the process is temporarily killed to save resources. But if you don't change the PendingIntent in any way, it should be no problem to register over and over again.
However, it is not possible to start the application when it is installed, there has to be some user interaction first.

onAlarm fired receiver

I want to create a broadcast receiver that will get notified when an alarm is started.
For example I set the alarm to 10 am and I go to sleep, then when the alarm is fired I want to have an receiver that will be notified.
Is it possible to do this ?, is there any intent that is fired on alarm start ?
For setting alarm use Alarm-manager class. You can set alarm using pending intent and using calendar you can set the time. Check the following code, in this AlarmReceiever is broadcast receiver which receives intent from pending intent at specific time that you can set in set method as second parameter .
Intent alaram=new Intent(FirstActivity.this,AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(FirstActivity.this, 0, alaram,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),pendingIntent);
//cal.setTimeInMillis(System.currentTimeMillis());
cant you use pendingIntent too!! just saying
It appears that there is no standart alarm used by alarm applications. So either you have to look up used alarm in your logs and use it ( this will be alarm application dependent, and
surely not portable ) or just use alarm manager and schedule your own alarm

Categories

Resources