473,327 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

How to set broadcast receiver attributes programmatically in android studio?

88 64KB
I'm broadcasting an intent in my app and receiving it with a broadcast receiver. I can handle the broadcasting and receiving. No problem with that. However, I want to register the receiver completely programmatically instead of doing it in the manifest file. Notice, that in the manifest file, there are two attributes of the receiver android:enabled="true" and android:exported="false". I need to know, how do I specifically set these two attributes when I register the receiver programmatically?

My AndroidManifest.xml file:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.     package="com.example.mybroadcastapplication">
  4.  
  5.     <application
  6.         android:allowBackup="true"
  7.         android:icon="@mipmap/ic_launcher"
  8.         android:label="@string/app_name"
  9.         android:roundIcon="@mipmap/ic_launcher_round"
  10.         android:supportsRtl="true"
  11.         android:theme="@style/Theme.MyBroadcastApplication">
  12.         <activity
  13.             android:name=".MainActivity"
  14.             android:exported="true">
  15.             <intent-filter>
  16.                 <action android:name="android.intent.action.MAIN" />
  17.  
  18.                 <category android:name="android.intent.category.LAUNCHER" />
  19.             </intent-filter>
  20.         </activity>
  21.         <receiver
  22.             android:name=".MyBroadcastReceiver"
  23.             android:enabled="true"
  24.             android:exported="false">
  25.         </receiver>
  26.     </application>
  27.  
  28. </manifest>
My MainActivity.java file:

Expand|Select|Wrap|Line Numbers
  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  2.     MyBroadcastReceiver myReceiver;
  3.     IntentFilter intentFilter;
  4.  
  5.     @Override
  6.     protected void onCreate(Bundle savedInstanceState) {
  7.         super.onCreate(savedInstanceState);
  8.         setContentView(R.layout.activity_main);
  9.         myReceiver = new MyBroadcastReceiver();
  10.         intentFilter = new IntentFilter();
  11.         intentFilter.addAction("com.example.mybroadcastapplication.EXPLICIT_INTENT");
  12.         findViewById(R.id.button1).setOnClickListener(this);
  13.     }
  14.  
  15.     public void broadcastIntent() {
  16.         Intent intent = new Intent();
  17.         intent.setAction("com.example.mybroadcastapplication.EXPLICIT_INTENT");
  18.         getApplicationContext().sendBroadcast(intent);
  19.     }
  20.  
  21.     @Override
  22.     protected void onPostResume() {
  23.         super.onPostResume();
  24.         registerReceiver(myReceiver, intentFilter);
  25.     }
  26.  
  27.     @Override
  28.     protected void onStop() {
  29.         super.onStop();
  30.         unregisterReceiver(myReceiver);
  31.     }
  32.  
  33.     @Override
  34.     public void onClick(View v) {
  35.         switch (v.getId()) {
  36.             case R.id.button1:
  37.                 broadcastIntent();
  38.                 break;
  39.             default:
  40.         }
  41.     }
  42. }
My MyBroadcastReceiver.java file:

Expand|Select|Wrap|Line Numbers
  1. public class MyBroadcastReceiver extends BroadcastReceiver {
  2.  
  3.     @Override
  4.     public void onReceive(Context context, Intent intent) {
  5.         if (intent.getAction() != null && intent.getAction().equals("com.example.mybroadcastapplication.EXPLICIT_INTENT"))
  6.             Toast.makeText(context, "Explicit intent received.", Toast.LENGTH_LONG).show();
  7.     }
  8. }
Regards
Mar 19 '22 #1
1 6509
priyamtheone
88 64KB
Studying further revealed the fact that there isn't any Java code counterpart for those two XML attributes. If you register the BroadcastReceiver it is enabled. If you unregister it, it is disabled. Those attributes are only relevant for manifest-registered receivers. If you dynamically register a BroadcastReceiver, it will be triggered based on the IntentFilter that you specify. If you want to prevent your BroadcastReceiver from being triggered by other apps, you can use LocalBroadcastManager for that.
Apr 6 '22 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
Nepomuk
by: Nepomuk | last post by:
Android development is a pretty broad field and no one article could ever hope to encompass everything there is to learn. For that reason, this article attempts to collect links to various tutorials,...
0
by: robertybob | last post by:
Hi. I am trying to write a basic app in Android Studio (my first attempt). I have an EditText field for a username and one for a password plus a button. I also have a WebView which will contain...
0
gautamz07
by: gautamz07 | last post by:
Hey guys, i am coming on here after a long while :) hope you all been doing fine. I just have a quick question anybody here every used a db like crouchDB to connect to a android application. how do...
0
by: hasnainahmad | last post by:
I am using android studio. While debugging my app I want to jump to the specific frame and start debugging from that frame, skipping the other frames in the frames tab. I want to see the frames...
0
by: 5402702 | last post by:
My question is do I need to have an android SDK package saved to start a new android project, even if I don't want to run it. And do I need the SDK to create an android virtual device in eclipse. My...
5
by: LemonFoam | last post by:
can anybody shown me how to build an step counter app using android studio that will count the number of footstep and plot the number of footstep in graphical form for every 15 minutes?
0
by: LemonFoam | last post by:
I had build an mobile app which consist of 2 activity as shown in figure below.It can let user to choose for go to main activity or the second activity Main activity let user to send text to the...
0
by: programmer3 | last post by:
Hi everyone! I need help transferring an image via TCP from a python program on my raspberry pi to an android application. I have set up a client-server architecture such that my raspberry pi 3...
0
by: Aslancheg | last post by:
Hello. I program on android studio. I do my messenger there and do not know how to add video calls and voice messages. Help please.
0
by: yakovcohen94 | last post by:
I have a problem with save the value of string or editText in java android. When I redirect from FirstActivity to Second and return after it to First, I need that a String that i fill earlier stay in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.