Broadcast receiver not working when app is closed
First of all you need to use the Service for this functionality to work.
In the Activity you can start and stop the service by using the below codes.
// to start a service
Intent service = new Intent(context, MyBrodcastRecieverService.class);
context.startService(service);
// to Stop service
Intent service = new Intent(context, MyBrodcastRecieverService.class);
context.stopService(service);
Then you can use the below service
public class MyBrodcastRecieverService extends Service
{
private static BroadcastReceiver br_ScreenOffReceiver;
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public void onCreate()
{
registerScreenOffReceiver();
}
@Override
public void onDestroy()
{
unregisterReceiver(br__ScreenOffReceiver);
m_ScreenOffReceiver = null;
}
private void registerScreenOffReceiver()
{
br_ScreenOffReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
Log.d(TAG, "ACTION_SCREEN_OFF");
// do something, e.g. send Intent to main app
}
};
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
registerReceiver(br_ScreenOffReceiver, filter);
}
}
Keep broadcast receiver running when activity and/or app is closed?
You're creating a Context-registered receivers, as per the docs:
Context-registered receivers receive broadcasts as long as their
registering context is valid. For an example, if you register within
an Activity context, you receive broadcasts as long as the activity is
not destroyed. If you register with the Application context, you
receive broadcasts as long as the app is running.
You can create your BR in a separate file and register it in your manifest file so it will be called when your intent filter is matched:
<receiver android:name=".wifiStateReceiver" android:exported="true">
<intent-filter>
<action android:name="android.net.wifi.STATE_CHANGE" />
</intent-filter>
</receiver>
When the intent filter is matched your onReceive method inside your BR will be called.
public class wifiStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//do some quick processing and call an activity
}
}
Keep broadcast receiver running after application is closed
You can use a service
In main app start/stop the service
Intent service = new Intent(context, MyService.class);
context.startService(service);
...
Intent service = new Intent(context, MyService.class);
context.stopService(service);
service
public class MyService extends Service
{
private static BroadcastReceiver m_ScreenOffReceiver;
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public void onCreate()
{
registerScreenOffReceiver();
}
@Override
public void onDestroy()
{
unregisterReceiver(m_ScreenOffReceiver);
m_ScreenOffReceiver = null;
}
private void registerScreenOffReceiver()
{
m_ScreenOffReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
Log.d(TAG, "ACTION_SCREEN_OFF");
// do something, e.g. send Intent to main app
}
};
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
registerReceiver(m_ScreenOffReceiver, filter);
}
}
Android how to keep alive broadcast receiver when app is closed
if you want your receiver to run even when Application is closed, start it from a service Run Receiver Forever
Keep connectivity broadcast receiver running after application is closed
I have just found the problem. There is a bug in Android 4.4.x which kills background services when closing the app. I was testing my app in Android 4.4.2. Here there is a detailed explanation:
http://www.androidpolice.com/2014/03/07/bug-watch-stopping-apps-on-android-4-4-2-can-silently-kill-related-background-services-a-fix-is-on-the-way/
So my code was right as I have tried in Android 4.2.2 and it works (I have tried overriding onStartCommand()
so maybe that was needed).
Broadcast receiver is not working only when app is closed in android pie
After spending few days I found the real issue.
My code was working fine on Android O and Android P both in foreground and background but when I clear the app from the recent apps list, it stops working on some devices because of
Manufacturers have added task manager feature by default which force stops the apps for memory/battery management. But few applications like Whatsapp, Facebook works. This can be because they would have whitelisted the most famous applications.
For more information follow the below link
clear Recent apps wipe the apps memory and my receiver stopped working
BroadcastReceiver doesn't work after app stops
I first try to register my broadcast receiver statically in Manifest. It doesn't work.
You cannot use manifest-registered receivers with LocalBroadcastManager
. Only receivers registered via LocalBroadcastManager.getInstance().registerReceiver()
will respond to local broadcasts.
My app needs to react on the broadcast, like send a server request after receiving success message or pop up a notification after receiving failure message.
Get rid of the broadcast and do that work in the service.
Related Topics
Findviewbyid Returns Null in a Dialog
Gson - Convert from JSON to a Typed Arraylist<T>
Unexpected Top-Level Exception: Com.Android.Dex.Dexexception: Multiple Dex Files Define
Error: Unable to Load Installed Packages Just Now
Import Maven Dependencies in Intellij Idea
Why Call Super() in a Constructor
How to Do a Junit Assert on a Message in a Logger
Junit: How to Simulate System.In Testing
How to Convert Object Array to String Array in Java
What Is the Default Access Specifier in Java
Date Object Simpledateformat Not Parsing Timestamp String Correctly in Java (Android) Environment
How to Scroll a Scrollview Programmatically in Android
Countdowntimer in Minutes and Seconds
How to Delete Stuff Printed to Console by System.Out.Println()