How to receive Broadcasts when app is off? - android

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

Related

How to get USER_PRESENT event even when the application is killed?

I want to check when a user Unlocks his device, and run a piece of code. How can I know If user unlocked?
I tried creating a broadcast receiver, registering it in the manifest with an intent filter for action USER_PRESENT. But from android Oreo restrictions were imposed on broadcasts.
I've tried an implicit broadcast receiver, but its life ends with the app getting killed.
"How to get unlock or USER_PRESENT event even after the app is killed"
can a background service be used here?
or any other broadcast?

static broadcast receiver above andorid N

I am creating a custom battery charger animation app.
I want to listen action ACTION_BATTERY_CHANGED using Broadcast Receiver. Broadcast will listen even if app is not on mobile stack.I do not want you use Foreground service as it always shows a notification and consume more battery.From android Oreo we can not declare all broadcast receiver on manifest.
In short a want to listen broadcast above android Oreo while my app in not on mobile stack and I do not want to use Foreground service.
Please suggest what should I do.

Using a broadcast receiver for alarm manager and notification

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.

Difference between Service and Broadcast receivers in android

I want to know the difference between services and broadcast receivers, can anyone point out an example that can be observed on android mobile devices.
Thanks
Service: If you want to do something in background , this will be running always in background even if the application closed. You can create this in separate process and also you can give your service to other app if you want. Downloading any content or Music is good example
Broadcast Reciever: Usually system will send some info which can be recieved by your app if you would wish to ,by registering. And you can do something what you want when that thing happens by using onReceive method.
Example is the system will send BroadcastReceiver when new sms arrives or Booting done
Here is good article : Service and BroadcastReceiver
Service is used when you want to do something in background, any long running process can be done using Service in Background. For example, you want to play music when your application gets close. In that case service will be running in background with music.
Example of Service
BroadcastReceiver is used when you want to fire some stuff or code during some event. For example, event can be on Boot of Device. If you want to perform something when device Boots, date and time changed etc...
Example of BroadcastReceiver
I think of it possibly a different way. A Service receives intents that were sent specifically to your application, just like an Activity. A Broadcast Receiver receives intents that were broadcast system-wide to all apps installed on the device.
(The reason I say a Service is a bit like an Activity is that: You wouldn't broadcast a message saying "start Activity MyActivity" across all apps installed on the device. It is only for your specific app.)
Of course, as others mentioned, a Service can continue running in the background, whereas a Broadcast Receiver should finish quickly (e.g. if it is running for more than 5 seconds it may be killed by the OS). The Broadcast Receiver can still run in the background (when app is closed) under certain circumstances. For this, it's worth mentioning that there are actually two types of Broadcast Receivers - Manifest-declared, and Context-registered. They have different lifespans and restrictions - the former can receive broadcasts in the background with certain restrictions, while the latter cannot receive broadcasts in the background (app must be running and active) but has no restrictions on the types of intents that can be received.
Both services and broadcast receivers must be specifically invoked (via an intent), but for services this is usually a specific call (e.g. when your app is started or when the user clicks some button) whereas for broadcast receivers they don't need to be explicitly started as they will start anyway when a relevant broadcast is made.
Here's how I would think of it:
Type
Displays UI?
Can continue running for a long time when app is closed?
Can receive intents when app is closed?
Intents must specifically target your app?
Restricted list of intents that can be specified?
Activity
Yes
No
Yes
Yes
No
Service
No
Yes
Yes
Yes
No
Manifest-declared Broadcast Receiver
No
No
Yes
No
Yes1
Context-registered Broadcast Receiver
No
No
No
No
No
1: Only if you target Android 8.0 or above. The restrictions are not applied if the intent specifically targets your app. The restricted list of intents can be found here.

What is BroadcastReceiver and when we use it?

What is a BroadcastReceiver? What are its uses and how can I use it?
Start by reading the documentation. Also, copying from Application Fundamentals:
Broadcast receivers
A broadcast receiver is a component that responds to system-wide
broadcast announcements. Many
broadcasts originate from the
system—for example, a broadcast
announcing that the screen has turned
off, the battery is low, or a picture
was captured. Applications can also
initiate broadcasts—for example, to
let other applications know that some
data has been downloaded to the device
and is available for them to use.
Although broadcast receivers don't
display a user interface, they may
create a status bar notification to
alert the user when a broadcast event
occurs. More commonly, though, a
broadcast receiver is just a "gateway"
to other components and is intended to
do a very minimal amount of work. For
instance, it might initiate a service
to perform some work based on the
event.
A broadcast receiver is implemented as a subclass of
BroadcastReceiver and each broadcast
is delivered as an Intent object. For
more information, see the
BroadcastReceiver class.
Finally, read in Common Tasks how you can utilize BroadcastReceivers to listen for messages and set alarms.
A broadcast is generated by android on occurrence of some action , BroadcastReceiver class enables the developer to handle the situation on occurence of the event/action . Action can be arrival of msg or call , download complete , boot completed , etc.
Broadcast Receivers simply respond to broadcast messages from other applications or from the system itself. These messages are sometime called events or intents. For example, applications can also initiate broadcasts to let other applications know that some data has been downloaded to the device and is available for them to use, so this is broadcast receiver who will intercept this communication and will initiate appropriate action.
I like this slide, because it focuses on Broadcast Receiver and offers simple description. The minor problem is that the updated date was a little bit old ( in 2011 ).
Link
Android Application Component: BroadcastReceiver Tutorial
(retrieved from the slide)
Broadcast Receiver
Receives and Reacts to broadcast Intents
No UI but can start an Activity
Extends the BroadcastReceiver Base Class
BroadCastReciever is an Android Component that helps you to know handle registered System Events or Application Events.
For Example:
System Events Such us : the screen has turned off, the battery is low, or a picture was captured.
Applications can also initiate broadcasts—for example, to let other applications know that some data has been downloaded to the device and is available for them to use... etc
In simple terms
A broadcast receiver is basically an interface that you can implement so that your app can subscribe to system changes like when the system has finished booting, or a charger is connected/disconnected or airplane mode is switched on/off etc.

Categories

Resources