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.
Related
I want to start my application (let's say BRUSH YOUR TEETH)on a specific time and day. And I don't want to write the alarm service any extra code for alarm part. Rather, I want to know if it is possible:
1- I use original alarm clock that comes shipped with phone to set up alarm with some name. Let's say Time= "6:30 AM on Monday" and Name= "PSSST!! BRUSH YOUR TEETH"
2- The Alarm will start to ring on Monday morning at 6:30 AM with that PSST!!... name
3- My question here, does this alarm broadcasts any INTENT (for instance like INTENT.ACTION.BOOTCOMPLETE) that can be received with BROADCAST RECEIVER so that I start my activity in OnReceieve method?
In short I am too lazy to write additional code and want to use the existing phone clock service.
Any possibility of success with my lazy approach?
It's not exact answer on your question. However, I would recommend to use AlarmManager:
http://developer.android.com/reference/android/app/AlarmManager.html
All you need to do is to create an intent for your app and schedule it. It is pretty much minimal (2-3 lines for setting it up, one receiver and definition of receiver in manifest).
You can check example here:
Alarm Manager Example
Is there any way to use AlarmManager to activate an alarm for a specific period of time? I have start-time and end-time values stored in the database. I want to start an alarm at start-time that will make the device silent and alarm should end at end-time when the device volume will be normal again.
One way is set alarm at start-time & then set another alarm at end-time. But the problem is the time period may overlap that will need additional logic to be implemented if I go with 2 different alarms(one at start-time, another at end-time). Is there any procedure in Android to cope with this situation? Or implementing logic is the only way to overcome this issue?
Why not just in your Intent for the PendingIntent pass an extra like "endTime" long type for the time you want to end the alarm. (im assuming its repetitive) then in your broadcast receiver get that extra, compare to System.currentTimeMillis() and if it is less then current time cancel the alarm and exit?
I want to start my application everyday at a particular fixed time.i tried the link
Start app at a specific time but didn't worked.
By use of AlarmManager you can declare the time. And use of BroadcastReceiver you can start the application at the time of reached. I suggest you to try Commonsware example application. There you should modify the code by yourself with your needs.
Hope this helps you lot.
You should achieve it using a service with AlarmManger for that.
First when user install your application then you should start Service then service start AlarmManager content a pendingIndent this pending intent fire on you specific time mention you in 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.
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.