I am having a broadcast receiver that starts a service in ICS. I have not activity associated with the code. The issue that I am facing is that I am unable to receive the BOOT COMPLETED broadcast in my receiver. Is there a round about solution for this?
In newer versions of Android, an application must have been manually started by the user at least once before any background stuff can be run, so you're going to need an Activity.
Related
I have an application on Android devices that listens for incoming calls (incoming call intent) through broadcast receivers and displays a popup on the screen when a call comes in, even when the application is completely closed. This works immediately on the emulator, but on some devices, the popup does not appear until 2-3 minutes after the call comes in or does not appear at all. The problem is that the broadcast receiver is unable to catch the intent. How can I solve this?
I couldn't make a try. because I couldn't find the root cause of the problem.
Creating an application in which whenever Power is connected or disconnected a small ringtone is played.
But the problem that i am facing is that the application is not working whenever i am not taking any launcher activity.
And when there is a launcher activity than the application is working well.
Sigh..vague and no code :(
Android does not allow BroadcastReceiver to receive some broadcast info if the appĀ“s process is not alive.It was designed to against the evil apps. If you have an activity running,your process is alive and so your receiver is allowed to receive the broadcast.
You can make a transparent activity and use startService to start a service in background,then finish the activity.As your service is running ,your process is alive,so the Android will let you to receive the broadcast.
The rest of your questions can be directed at the offical docs.
I developed an app that receives the boot completed intent. From my tests I concluded that I must open the app at least once to start receiveing this intent.
Is this the correct behavior? If so, is there any way to start receiveing intents without starting my app at least once?
Thanks,
-George
The app must be opened before any code can be triggered, there is no way around this.
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.
I have an app on Android that reacts to incoming calls.
Now, since the OS shuts my app down whenever it want,
I need a way to to listen to the incoming calls and launch the app when it happens.
Will a BroadcastReceiver help? (just like launching on device restart)
Any idea?
thanks!
I think you have answered your own question. This is just the sort of thing a broadcast receiver is meant for. If the receiver is registered in your manifest then the application does not have to be running.
It will be automatically started when a matching intent is broadcast. Typically the response will be to update content or activities, make notifications with the Notification manager or launch/manipulate services.
Note that there is a 5 second execution limit in the BroadcastReceiver onReceive handler to ensure you do not try to do any 'heavy lifting' in it. Exceed this and a force close dialog will be displayed.
Yes a BroadcastReceiver would to the job as it will fire even you app is not running.