Controlling Alarm from 2 diff applications - Android - android

I need help in my android app development.
It goes something like this,
I will be having two separate applications (2 projects). In one application, i have to start a repeating alarm and in the other application i have to cancel the same alarm that was started in the first application.
The Android documentation says, the same pending intent and the intent object that was used to start the alarm
should be used the cancel the alarm.
So in this scenario, the pending intent and the intent object that was used to start the alarm will belong to application1 so i cannot used the same objects in application2
How do I proceed?
In summary -
The problem is, I need to start a repeating alarm from one application and i have to cancel the same alarm from another application.
Can this be done. If so, How?
Thanks in advance.
ifreeman

It is not that straightforward. Only original activity can cancel the alarm.
So I think you can configure a custom broadcast. When the second activity needs to cancel an alarm it will send this broadcast. The first activity will be listening to the broadcast and cancel the appropriate alarm on receiving it.

I guess you can do. Alarms are considered as same if intents passed to them via pending intent are same. filterEquals method of Intent class defins if intents are same or not. If intents are same then alarms are same so u can cancel that alarm. Check once.

Related

Can we startForegroundServie from an exact alarm's receiver in the background?

In one of the exemptions of the "cannot start foreground service from background" restriction, the doc mentions:
Your app invokes an exact alarm to complete an action that the user
requests.
Does this mean that the usage scenario below can work?
Use AlarmManager.setAlarmClock to schedule an exact alarm to trigger at time A. The alarm carries a pendingIntent that targets a registered broadcast receiver.
Time A hits, the receiver gets the intent.
In the receiver OnCreate method, we attempt to startForegroundServicewhich involves displaying a sticky notification and playing custom music with MediaPlayer.
I have implemented and tested this and it appears to be working, so I assume this is a valid use case.

How do you cancel a specific alarm manager after app is restarted?

I'm creating an app that, after receiving a text from a certain number, starts a repeating alarm using AlarmManager. The AlarmReciever plays an alarm sound for thirty seconds and then the alarm repeats every five minutes. I want to cancel the AlarmManager when the app is closed and restarted by the user but I have to use the same instance of the alarmIntent to cancel it.
I have to use the same instance of the alarmIntent to cancel it.
No, you have to use an equivalent PendingIntent to cancel it. By "equivalent", I mean:
It is the same operation (e.g., activity, service, broadcast)
It has the same request code (2nd parameter to methods like getActivity())
It has an equivalent Intent
By "equivalent Intent", I mean that all the routing information is the same (component, action, data, MIME type, categories). Extras do not matter.
You need to hold onto enough information in a persistent data store (e.g., file) to be able to create an equivalent PendingIntent to pass to cancel() on AlarmManager.

Should I use PendingIntent.getService() or getBroadcast with AlarmManager?

My app needs to grab some data from the web at a specific time each day. So I use an AlarmManager to schedule the task and that works ok.
But when looking at various examples there seems to be two ways to deal with the AlarmManager when it comes to the pending intent.
One uses PendingIntent.getBroadcast() to call a broadcast receiver when the alarm goes off and inside that receiver the service to do the real work is started.
Another approach is to use PendingIntent.getService() and call the service directly when that alarm goes off.
Can someone explain to me the difference between the two approaches so I can decide on which one to rely?
EDIT: One more question is where to acquire the wake lock when using getService()?
For example, when using a BroadcastReceiver I have the following line in onReceive():
WakeReminderIntentService.acquireStaticLock(context);
How should I acquire the wake lock if I instead call the service directly like:
PendingIntent pi = PendingIntent.getService(this, 0, new Intent(this, OnAlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
Should I simply acquire it from within the service instead?
One uses PendingIntent.getBroadcast() to call a broadcast receiver when the alarm goes off and inside that receiver the service to do the real work is started.
it has one more step in starting service than
Another approach is to use PendingIntent.getService() and call the service directly when that alarm goes off.
then you should use the second approach as it is reducing your one step in execution..
Reading your edit I presume you found out yourself: If you want to make sure that your service is started when using AlarmManager, you better take the detour of first sending to a receiver and acquiring a wake lock there.
Otherwise it is possible that the phone will sleep before the requested service is launched.
That's what the javadoc of AlarmManager says and I also read it in post by Google engineer.
So now for your edit: When to acquire the lock?
The whole point of using the receiver is to acquire the lock within the onReceive() method of the receiver, because Android will not fall asleep during the execution of this method.
For an example see this question.

Android PendingIntent howto

Ok I am stuck. Can someone please point me in the right direction? I have no idea where to start with pendingintents. I need to start custom service that sends some data back to the activity that started it. How would I do that?
I would probably register a Broadcast Receiver in my Activity and if I needed to communicate from Service to Activity, send a broadcast from the Service and the Activity's receiver would pick it up as long as the Activity was currently running. This, by the way, would not require the use of a PendingIntent. PendingIntents are more used with alarms from the AlarmManager or with Notifications.
You should consider using AsyncTask if you expect your Activity to be active (visible) all the time the Service runs.

Android: How to start an activity at a specified time?

I'm making an alarm clock of sorts, and I was wondering what the best way to start an activity at a certain time would be. Would it be using the broadcast service or...
You need to use AlarmManager to register an Intent that will fire at the specified time.

Categories

Resources