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.
Related
I have a widget that implements a specific broadcast receiver service to detect when the wifi connection goes down.
It works perfectly if the main activity is running or not.
The issue I have is when I stop the main activity then the broadcast receiver service stops as I don't detect anymore the wifi changes.
Is there a way to startservice that survives the main activity?
if not, any other mechanisms?
Probably you need to register your receiver in manifest and mark it as exported=true, so it can work if the app is not running. Check this link for detailed info please https://developer.android.com/guide/components/broadcasts
Why have I been down voted?
I've found a workaround: Broadcast Receivers started in an alarm manager are not stopped (when the main activity stops) in O. So I start an AM with a high repeatin interval.
I am working on internet calling app. In my app I am using to BroadcastReceiver to invoke call screen when app receives FCM data message with some specific keys. Normally is working fine, but if I swipe out my app from recent apps list it is not working. It seems like my BroadcastReceiver stops working after swiping app because all the FCM services are still and it receives FCM messages too and showing in logcat too.
I am registering my broadcast receiver in java code (not in manifest).
I used a service class to register broadcast receiver and also overrided onTaskRemoved() and used AlarmManager to keep service running but its not working too.
Why the BroadcastReceiver won't be fired and how to solve the problem?
I am registering my broadcast receiver in java code (not in manifest).
That's the cause of your problem. When user swipes away your app from recents list, the process of your app is being destroyed. Thus, your BroadcastReceiver is being destroyed too.
Register your BroadcastReceiver in AndroidManifest, then onReceive() will be called regardless your app has a running process or no.
I've registered the Intent.action_screen_off/on in my oncreate method of my application, and i can receive the events when my application is running. But when my application exits i get an broadcast intent leaked exception (off the top of my head i think thats what its called, either way its caused by not unregistering my receiver when my app exits)
How can i receive these events when my application is not running? i'd prefer not to use a service or something like that, i've seen other apps do it without services or background processes.
i've seen other apps do it without services or background processes.
That's not possible. The only way to register a BroadcastReceiver other than in running code is to declare it in the AndroidManifest.xml with the relevant <intent-filter>. As it's not possible to do that for either Intent.ACTION_SCREEN_OFF or Intent.ACTION_SCREEN_ON, the apps you've seen MUST have some running component(s) in order to receive the broadcasts.
In short, use a Service - I'm not quite sure why you say you'd prefer not to.
I currently have 2 apps in the market, let's call them app A and app B.
When a certain function is executed in app, I need something to trigger an event in app a, even if app a isn't currently running. I assume app B would send a broadcast message to app a and app a would need a broadcast receiver but I am not sure how this can be done, if it can be done.
There are basically two ways afaik:
Broadcast Receiver and using the sendBroadcast method on the sender side
or by using Intents:
you can use startActivity(Intent) even with another app, but this will bring the app to foreground rather than doing a job in background.
Use intents if the calling app should dissappear and the called app should be in foreground and use broadcasts if you just want a background task performed by another app
Can be done, even if the app isn't running, with BroadcastReceiver, just like you said.
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.