Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Broadcast receiver not executed on application close

I have an android application, where I schedule to an event (location update) to be executed in the future using Alarm manager. The scheduled event executes as expected as long as the application runs in the foreground or in the background. But once I force close the application under task manager or when android system kills the application due to memory issue when the app is at background, I am no longer able to receive the broadcast from the alarm manager.

As suggested by various posts and blogs i tried using 1) Intent.Flag_Include_Stopped_Packages 2) receiver android:process=":remote" in manifest 3) receiver android:exported="true" in manifest

In Service:

Intent locationIntent = new Intent("com.dummy.intent");  
locationIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
locationIntent.putExtra(LocationLibraryConstants.LOCATION_BROADCAST_EXTRA_LOCATIONINFO, locationInfo);
context.sendBroadcast(locationIntent, "android.permission.ACCESS_FINE_LOCATION");

In Manifest:

<receiver android:name=".NearestStationBroadcastReceiver" android:enabled="true"
  android:exported="true" 
  android:process=":remote">
    <intent-filter>
        <action android:name="com.dummy.intent" />
    </intent-filter>
</receiver>

Can someone please help me out?

like image 540
user3047032 Avatar asked Nov 28 '13 18:11

user3047032


People also ask

Does broadcast receiver work in background?

ACTION_BOND_STATE_CHANGED)); The alternative is to register the receiver in the AndroidManifest. This ensures that the BroadcastReceiver runs all the time, even when the app is backgrounded. I chose this option because I want my application to receive Bluetooth events even when the app isn't in use (i.e. backgrounded).

Which broadcast still runs even after app is in background?

IntentService runs outside the application in a background process, so the process would run even if your application is closed.

Is broadcast receiver deprecated in Android?

What does saving battery have to do with BroadcastReceivers? With Android Nougat, the support for registering three different implicit BroadcastReceivers in your AndroidManifest was removed. With apps targeting Android O, all implicit BroadcastReceivers registered in the manifest (except these) will stop working.

What is the difference between static and dynamic broadcast receiver in Android?

There are mainly two types of Broadcast Receivers: Static Broadcast Receivers: These types of Receivers are declared in the manifest file and works even if the app is closed. Dynamic Broadcast Receivers: These types of receivers work only if the app is active or minimized.


1 Answers

But once I force close the application under task manager or when android system kills the application due to memory issue when the app is at background, I am no longer able to receive the broadcast from the alarm manager.

These have nothing to do with each other, so if you have been simulating "when android system kills the application due to memory issue" by using Force Stop, that is your problem. An app that has been force-stopped through Settings has its alarms removed, among other things. A better way to simulate your process being terminated is to terminate it from DDMS.

1) Intent.Flag_Include_Stopped_Packages 2) receiver android:process=":remote" in manifest 3) receiver android:exported="true" in manifest

None of those are related to your problem, and android:exported="true" (and your use of an <intent-filter>) raises security issues, as now anyone can cause your BroadcastReceiver to be run at any time, for any reason.

Here is a sample application that successfully processes alarm events, even after the process has been terminated by DDMS.

like image 123
CommonsWare Avatar answered Oct 02 '22 04:10

CommonsWare