Android Oreo, and what to do about implicit broadcasts


I’ll keep this post short. In the middle of trying to re-write code for my apps to support the newer Android firmware, in this case Android Oreo and up, I faced many (an understatement) problems trying to find workarounds and functional fixes. Thankfully, many developers have written and shared their problems, fixes, and migration strategies for all sorts of topics. Specifically, background execution, usage of Services, Broadcasts, MediaPlayer, and several others just to name a few.

Initially, when I was doing Implicit Broadcast, I was using it for the purpose of a countdown timer, my app crashed because I was doing it in a class that inherited JobIntentService, so I needed to run the code that was calling sendBroadcast() with a Handler like so:

Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {

	@Override
	public void run() {
	    countdownTimer();
	}
});

Even then, I was getting an error like this: https://stackoverflow.com/questions/50040617/background-execution-not-allowed-receiving-intent-boot-completed

While the discussion on StackOverflow helped with reviewing my own code, I knew I had to search around further to see what I was doing wrong when I was calling sendBroadcast() as implicit Broadcasts were now deprecated in the newer Android firmware. So looking further into the LogCat message:

Background execution not allowed: receiving Intent {{ act=android.intent.action.BOOT_COMPLETED }}

I came across a fantastic article that definitely helped my understanding, and also guided me to solve my own problem with using Implicit Broadcast in Android. Originally, I was calling sendBroadcast() using LocalBroadcastManager.getInstance(this).sendBroadcast(). From my understanding, the reason why this doesn’t work is because I was sending broadcasts between app components like Services and Threads, hence I should have been using a explicit broadcast.

More importantly from the perspective of the bigger picture, the newer Android firmware has had many changes and limits regarding background processes. Apparently, there have been issues with devices under memory limitations getting into bad thrashing states, caused by broadcasts being sent frequently.

Read more here: https://commonsware.com/blog/2017/04/11/android-o-implicit-broadcast-ban.html

Hope this helps.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s