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
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);
}
}
Broadcast receiver not working background on some devices
You probably need to add your app to the list of "protected" apps, or the list of apps that are allowed to run in the background. On many low-end devices, and devices from Xiaomi, Huawei, etc. Android will not start (or restart) apps automatically unless they have been explicitly added to this list. Look in Settings under Battery, Power Management or Security.
See the following for more info:
- BroadcastReceiver works for a while, then stops
- "Protected Apps" setting on Huawei phones, and how to handle it
why Broadcast receiver is not working continuously inside service
I've changed my code. I've stared the foreground service after which my code is working and broadcast receiver also working even after closing the application.
Foreground service in main Activity-
Intent serviceIntent = new Intent(this, LatestCallService.class);
serviceIntent.putExtra("inputExtra", "Foreground Service");
ContextCompat.startForegroundService(this, serviceIntent);
Android Manifest file-
uses-permission android:name="android.permission.FOREGROUND_SERVICE"
This way you can see whether your service is working in the background or not.
Hope that helps!! :)
Broadcast receivers in background when app is not in recents
Once your MainActivity
is destroyed, your BroadcastReceiver
goes away. This will occur:
- If the user presses BACK, or
- If the user navigates away by some other means (e.g., presses HOME) and your process is terminated
You seem to be defining "the app is closed" as being when the user swipes the app out of the overview screen (recent task list). That usually terminates the app's process, so your BroadcastReceiver
will be gone at that point.
Related Topics
Converting Exponential Value in Java to a Number Format
Check If Url Is Https or Http Protocol
Multiple Scanner Inputs (Java)
Sharing a Variable Between Multiple Different Threads
How to Do Post Request With Raw Data Via Spring Rest Template
Passing Empty List as Parameter to JPA Query Throws Error
Java Ssl: How to Disable Hostname Verification
How to Sort Arraylist of Objects by Timestamp and Get Last Five Elements
How to Mock JPA Repository'S Save Method in Unit Tests
How to Split a Huge Zip File into Multiple Volumes
How to Resolve Java.Lang.Noclassdeffounderror: Javax/Xml/Bind/Jaxbexception
Iterate Through Each Digit in a Number
How to Get the Path of Src/Test/Resources Directory in Junit
Handling the Null Value from a Resultset
Converting Excel File (Xlsx) into Json
Read Huge Excel File(500K Rows) in Java