Broadcast Receiver Is Not Working When Application Is Removed from Background

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



Leave a reply



Submit