Using a broadcast receiver for alarm manager and notification - android

I created an alarm clock app that plays music when the alarm goes off. I set up a notification as well. I am using a broadcast receiver but it is ended when I leave the activity that created it and plays the music. My first question is, is a broadcast receiver the best way to handle this and is implementing a receiver in the manifest the only way to eliminate my problem of losing my receiver. When I leave the activity that created the app, I receive an error because I haven't unregistered the receiver. My first app and needing some advice. I can provide the code if needed.

You could use the inbuilt alarm to trigger using a pendingintent. That way you don't need a broadcast receiver.

Related

Android, should I use a service or just broadcast Receiver?

for context I am creating an android app that will allow users to set multiple alarms that will notify them with notifications in future to do something and I was wondering the best way to achieve this.
Currently I'm going to use an alarm manager and broadcast receiver to achieve this but I wondered if I should create a service for this or if that was redundant for my intents
Any help appreciated, Cheers.
There is no need of creating your own service class or using intent service, as per your given requirements you can rely on Broadcast receiver and alarm manager to notify a person at specific time. what else you can do to add BOOT_COMPLETED permission in your manifest to wake the receiver after rebooting your phone.

Android: How to start activity when phone is sleeping, similar to Viber incomming call activity

I am developing an app and this app needs to give a clear indication to the user when some event happens.
Only thing I could do until now is giving a notification in the notification area. But, I need to give a more visible notification, similar to the behavior when phone is ringing in an incoming call.
As I can understand, the reason why android is only allowing apps to give a notification is to prevent apps from disturbing the user. But, this app I am developing plays a vital role in the job of the user, so I don't think it is inappropriate to give a such strong notification.
I know it should be doable since apps like Viber can start an activity similar to a incoming phone call, even when the device is sleeping.
Does anyone know how to get this done?
Register a broadcast receiver, and add a custom action to it say CustomAction.Instead of showing notification, throw a broadcast and add CustomAction via intent filter.
Now in the onReceive method of broadcast listener, check
if(intent.getAction.equals("CustomAction"))Intent i = new Intent(context, YourActivity);
context.startActivity(i);
Sorry for not a formatted answer, I'm driving, will update it later for more clarification.
Update
Register broadcast receiver in a sticky service. So that service can be started automatically if killed and register broadcast register again.
Don't forget to unregister broadcast receiver in onDestroy() method of service and also in YourActivity when you purpose is resolved.
Just adding a sticky service (which does nothing) fixed the issue. Adding the service prevented the process getting killed when user exits the app and removes it from recent app list.
Because of the service, the app process is running even when a no UI is visible. In this state, if an activity is shown from the GCM service, it gets shown.
You can trigger a broascast as Vinay mentioned. If it still does not work, try using wake-locks. These wake-locks help in waking the device when it is in sleep mode. It will act like force wake and after calling wake-locks, you can perform your actions.
Hope it helped..
Thanks.

How to receive Broadcasts when app is off?

How can I make an app respond to ACTION_SCREEN_ON broadcasts even when the app is off?
I have been reading about alarms and IntentService but I am not sure what the best practice is.
I am trying to make an app that takes pictures whenever the screen is unlocked.
Register your BroadcastReceiver in a service.
From Broadcast Receiver within a Service

what is the benefit of using a BroadcastReceiver in android?

This might be a foolish question for you, Please consider that im not an expert in Android programming.
I had implemented a PhonestateListener with an inline code in my Service.But after following the broadcast tutorial from here.I saw the same phoneStatelistener is explained with the help of a BroadcastReceiver.Can anyone pls explain me the benefits?Thanks.
A Broadcast receiver wakes your application up, the inline code works only when your application is running.
For example if you want your application to be notified of an incoming call, even if your app is not running, you use a broadcast receiver.
If your application is playing audio, and you want to stop the music on an incoming call, you use the inline code.
A broadcast receiver also called as receiver is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android run time once this event happens. Broadcast receiver is also called the gateway between your application and outside world.
example of broadcast receiver is when new sms arrives, broadcast receiver sends notification to messaging app and small icon pops up in notification bar.
rules of broadcast receiver:
it has maximum limit of 10secs,
do not do any asynchronous operations which may take more time,
do not do heavy database operations or networking operations in broadcast receiver.

How to notify a user with SMS or notification when needed

So in my app I'm looking to have the students class schedules and the times which they need to be in class.
When the app is turned off I still want the notification to pop up that their class is about to start (possibly vibrate).
This sort of functionality is very akin to an incoming text message or notification of something like an email. I was wondering how to implement that into an app?
You need a Service which can run when the app is not opened. You should also think about a BroadcastReceiver that listens to BOOT_COMPLETED
You will need to use the AlarmManager to set an alarm when you require the notification, possibly with the RTC_WAKEUP flag so the device will wake from a sleep. From your alarm receiver you will need to take a wake lock (if you used RTC_WAKEUP) and start a service that will use the NotificationManager to display a message to the user (very similar to the incoming SMS message).
As #WarrenFaith pointed out you will need to create a BOOT_COMPLETED receiver to re-establish the alarm after the phone is rebooted as they are not persistent.

Categories

Resources