Receive alarm in spite of that device has been restarted - android

I want to make an alarm clock. I've done it but it works only if device wasn't restarted.If it was I just don't get any alarm. What should I do to receive my alarm no matter has device been restarted or not?
That's how I set alarm time:
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,calendar1.getTimeInMillis(),pi1);

This is what I tried in a similar situation:
Make your broadcast receiver detect the system restart. Now, as soon as your system restarts, your broadcast receiver will detect it and you can perform your task. Like, in my project, I ran a query onto my SQLite database and retrieved all the alarms that have not executed and then executed them according to my requirements and updated the database with the same.
You can try something similar to it.
Code to retrieve alarms:
Add your Broadcast Receiver with the following attributes:
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
This BroadcastReceiver will detect your Android device's restart and execute the onReceive() method of the BroadcastReceiver. In the onReceive() method, retrieve all the alarms according to your criteria and then process them further.
To learn further about how to retrieve records or tuples from the SQLite Database go through the following link:
http://developer.android.com/training/basics/data-storage/databases.html#ReadDbRow

Related

Android: Fire all missed alarms when device turns on

In my application the user has set some reminders and I have to alarm them when the time has come. Currently I'm using AlarmManager.RTC as a type of AlarmManager
am.set(AlarmManager.RTC_WAKEUP, date.getTimeInMillis(), pendingIntent);
it in working as per instruction
// Sets an alarm - note this alarm will be lost if the phone is turned
// off and on again
is there any way the I can fire missed alarms when I turn on the device?
Please note that I don't want to wakeup the device, I just wanted to remind them when they turn on the device.
PS: I have read the documentation http://developer.android.com/reference/android/app/AlarmManager.html#RTC, but I didn't find my option.
is there any way the I can fire missed alarms when I turn on the device?
Step #1: Have some way of tracking, yourself, what is missed, such as by tracking event status in a database.
Step #2: Set up a BOOT_COMPLETED BroadcastReceiver to find when the phone is rebooted. In there, schedule any alarm(s) that are still in the future, and decide what to do about alarms that occurred in the past but were missed (e.g., raise a Notification pointing out the missed events).
You would have to do this manually.
One way to do this would be to listen for the phone shutdown event:
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
and before the device shuts down, save the shutdown time.
Then listen for device bootup:
<action android:name="android.intent.action.BOOT_COMPLETED" />
and write your own logic that determines which alarms would have fired during that downtime, and fire those alarms immediately with AlarmManager.
AlarmManager will alarm all of your alarms till your phone is on.
Registered alarms are retained while the device is asleep (and can
optionally wake the device up if they go off during that time), but
will be cleared if it is turned off and rebooted.
Now that only way fits is to kepp tracks through database.
Put an additional class that extends BroadcastReceiver. Where in its OnReceive() method you can re-generate your alarms when device reboots. (obviously if you have some track, i.e database).
Setting BroadcastReceiver for your purpose can be seen here How to start an Application on startup?.
Hope this helps.
Register broadcastReceiver in your Manifest.xml
<receiver
android:name=".AlarmRestartReceiver"
android:enabled="true">
<intent-filter>
<action
android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
After this, listen event when device wake up.
override fun onReceive(context: Context, intent: Intent?) {
if (intent?.action == "android.intent.action.BOOT_COMPLETED") {
// re-register all alarm here..
}
}

Schedule AlarmManager Android

Can I schedule alarm manager (.set() methode) in android to a time which is one month later the current time
will it be alive for that mutch of time ?
what will be the OS effects on this alarmManager ?
Tip for a case when user reboots his phone. You can declare BroadcastReceiver with the following intent-filter:
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
and re-schedule your alarm in onReceive(...) of your receiver class. Intent will be delivered to the receiver after user reboots his phone. Don't forget to add the following permission to your manifest file:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Or you can use the library that will handle such cases for you:
https://github.com/commonsguy/cwac-wakeful
Theoretically yes, If the user keeps his phone on for that long, however the alarm doesn't survive reboots and will therefore not be scheduled when the phone restarts.

SMS Receiver as a Service

I have created SMS Receiver app... but i want to create it as an service, it should run in the background (i.e no separate UI for this app, want to work like alarm app) and even if mobile restarts it automatically starts... could any one help on this?
My previous SMS Receiver app code was here
Unable to instantiate activity ComponentInfo in Android Receive Sms App
it should run in the background
Your existing BroadcastReceiver for the (undocumented) android.provider.Telephony.SMS_RECEIVED already runs in the background.
and even if mobile restarts it automatically starts
Your existing BroadcastReceiver for the (undocumented) android.provider.Telephony.SMS_RECEIVED already is available after the device reboots.
If you want your service to run at phone startup, you should simply declare a broadcast receiver with this intent filter:
<receiver android:name="MyStartupIntentReceiver">
<intent-filter>
<action
android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
In the broadcast receiver onReceive() method, just launch your service:
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent();
serviceIntent.setAction("myPackage.MyService");
context.startService(serviceIntent);
}
And be sure to link the service in your manifest with the same name of the intent you launch in the broadcast receiver:
<service android:name="MyService">
<intent-filter>
<action
android:name="myPackage.MyService" />
</intent-filter>
</service>
As CommonsWare has said there is no need to actually have a "Service" run in the background for you to receiver SMS broadcasts, if registered in your manifest properly your BroadcastReceiver with "android.provider.Telephony.SMS_RECEIVED" as the intent filter will fire every time an SMS is received no other action required.
Depending on what exactly you want to do, from that broadcast receiver you can then do you work via an actual Service, or probably a better option would to be to use an IntentService. This is because the thread used for the broadcast will get killed shortly after starting, so you should not do any extensive work in it.
It is generally recommend to not use an actual "Service" unless explicitly required....BUT if that is what you need then you need Davide should get you the right direction.

Notification gets disappeared when phone is rebooted

I am working on alarms and making an app for task reminder.
I am showing notification at scheduled time by user.
But when alarm rings , notification appears . Then I switched off my phone , and again when I switched , on the notification get disappeared(doesnt show the notification).
while in the case of SMS notifiaction(default android implementation) it does not get disappeared until we drag the notification.
I want the same as like sms notification.
What should I do?
Notifications are not persistent across device boots. If they reappear for certain apps, it's because the app is starting on boot and re-creating them.
You should define a receiver that determines if the notification should be present and creates it if necessary, and start this receiver on device boot.
Set its intent filter for,
android.intent.action.BOOT_COMPLETED
and use permission,
android.permission.RECEIVE_BOOT_COMPLETED
and make sure your receiver has permission,
android.permission.RECEIVE_BOOT_COMPLETED
Like this,
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
...
<receiver ... android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>

Android - Having a service run every day at 4AM

I would like to know the best practices for running a Service every day at 4AM.
The way I think I should be doing it is to create a new repeating alarm using AlarmManager and having it run the service at 4AM. Problem is, I'm not sure where to put the code to set the alarm.
Do I do it in my main activity as one of the first tasks in the OnCreate method? Do I do some funky stuff with BroadcastReceivers and intents? What happens when a user updates my app? What happens when a user restarts?
Any help with these questions would be much appreciated :) Sample code would be helpful as well!
Bara
You can schedule your alarm each time phone boots and each time your application starts. To listen to phone boot event you can use BroadcastReceiver.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
.
.
.
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
For a complete sample you can take a look at Photostream application http://code.google.com/p/apps-for-android. It uses exactly the same approach.

Categories

Resources