Tech Blog

Coding

How to Restrict Broadcast within an Application in Android

By Satyabrata Dash1 min read
← Back to blog

Whenever we initiate an event through a broadcast in our application then that broadcast is announced system wide. So any application who is listening to the broadcast with the same action and category that our application is broadcasting  then that application will also be notified about the broadcast.

 Is there a way to restrict broadcasts within the application so that the broadcast will be announced within my application? Well LocalBroadcastManager is the answer to that.

Below is a code snippet to initiating a broadcast from our application:

Intent lObjIntent = new Intent();
lObjIntent.addCatagory(Intent.CATAGORY_DEFAULT);
lObjIntent.setAction(“com.myexample.demo”);
sendBroadcast(lObjIntent);

With the above code any application installed on the same android device with BroadcastReceiver with action “com.myexample.demo” will be able to receive broadcast.

How to restrict the broadcast so that only my application’s broadcast receiver will be notified?

LocalBroadcastManager lObjLocalBroadcastManager = LocalBroadcastManager.getInstance(getApplicationContext());

Intent lObjIntent= new Intent();
lObjIntent.addCatagory(Intent.CATAGORY_DEFAULT);
lObjIntent.setAction(“com.myexample.demo”);
lObjLocalBroadcastManager.sendBroadcast(lObjIntent);

In the above code we have used the instance of LocalBroadcastManager to achieve our goal to send the broadcast within our application. So the same broadcast with the same event will not be delivered to other applications.

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 →