why my widget broadcast receiver service stops when the main app stops - android

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.

Related

BroadcastReceiver is not working after swiping app from recents list

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.

What is the difference between registering BroadcastReceiver in code and in manifest?

What is the difference between registering BroadcastReceiver through code in an Activity and in Android manifest? Also, can Service be started from manifest, without calling startService() from code?
I would like to start all the BroadcastReceivers and Services on boot completed. But, when I reboot my device, some of them start, some of them don't. All are added to manifest and are working when I build application.
what is the difference registering receiver through activity and in android manifest?
Receivers declared in manifest always active, registered in activity - active only after registration and will die with app process.
can service be started from manifest, without call startService() from code?
No
Start all services inside BootFinishedReceiver (or whatever you called it)
Also, it's good practice to register only one receiver for all broadcasts.

Do not want to keep a launcher activity in BroadcastReceiver

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.

android wifi state changed with broadcast receiver

If the user has left the app via the back button key and the app contains a broadcast receiver for wifi state changed will a change in the wifi state trigger the 'left' app receiver and resume the app? Or will the receiver only work while the app is visible?
If you have registered for the intent-filters in your AndroidManifest.xml, the receiver will be called regardless of whether or not your app is in the foreground or even running. The BroadcastReceiver will be garbage collected the moment onRecieve is done though, thus you are not allowed to do any threading in the BroadcastReceiver. You are limited to starting an activity or binding to a service.

i want run service and broadcast receiver after the force close the application

How can I automatically restart my service and broadcast receiver after the application gets force close due to some error. Since I'm using inbuilt applications like calender and events. Some of the mobile doesnt support and getting force closed. How to handle those things and restart those service and broadcast receiver.
Thanks
Register your receiver in the AndroidManifest. That way it will be called even when your app is not running: R.styleable.AndroidManifestReceiver
You could then check in the BroadcastReceiver whether the service is still running and restart it.
See for example the class SmSForwarder here. The broadcastreceiver does not need to run because it is registerend in the AndroidManifest.xml (see line 29). Android will start the Broadcastreceiver as soon as the Intent "android.provider.Telephony.SMS_RECEIVED" is sent.

Categories

Resources