Tech Blog

Coding

How to run Broadcast Receiver on Worker Thread?

By Bvmg karthik1 min read
← Back to blog

When you Register for any Broadcast receiver by Default onReceive() method will be called on the Main Thread( UI Thread). But If we want to do any heavy operations or if your broadcast is calling multiple times, then so much load will be accumulated on Main Thread which is not recommended. To overcome this, the ideal solution is to move the entire operation on the worker Thread. To achieve this while registering the Broadcast receiver itself, we can provide the scheduler to registerReceiver() variant.


Context.registerReceiver(BroadcastReceiver receiver, IntentFilter filter, String broadcastPermission, Handler scheduler)

broadcastPermission allows you to apply for permissions on who can broadcast intents to your receiver and scheduler allows the receiver to run in a different thread.

The below snippet of code is to create a Handler by using the HandlerThread and register a Broadcast using that.


HandlerThread broadcastHandlerThread = new HandlerThread("BroadcastRegisterThread"); broadcastHandlerThread.start(); Looper looper = broadcastHandlerThread.getLooper(); Handler broadcastHandler = new Handler(looper);

registerReceiver (receiver, intentFilter, broadcastPermission, broadcastHandler);

this is how we can register the receiver in the context of the scheduler.

Keep reading

You may be interested in

Browse all articles →
Active Directory Federation Service
July 5, 20235 min read

Active Directory Federation Service

Active Directory Federation Service (ADFS) uses claims-based identity model to maintain application security and to implement federated identity. ADFS is a concept of Windows Server operating system, that provides users with single sign-on access to the application, those are located across organizational boundaries. For Example: We have 2 organizations – Organization 1 and Organization 2. […]

By Abinash MishraRead More →
Remote Access to Microsoft SQL Database Server
April 5, 20232 min read

Remote Access to Microsoft SQL Database Server

Remote access to a SQL server is important because it allows users to access the server from anywhere, at any time. This enables remote workers to access the data they need when they need it, even if they are not physically in the same location as the server. It also allows IT admins to access […]

By Malkeet SinghRead More →
Types of Load Balancer in AWS
February 6, 20235 min read

Types of Load Balancer in AWS

What is Load Balancing ? Load balancing is a critical component of any scalable and highly available Cloud or On-premise architecture. It helps distribute incoming traffic across multiple servers, improving the availability and scalability of a website or application. In the cloud, load balancing is offered as a service by various cloud providers such as […]

By Keerthik ShenoyRead More →