CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2008
    Posts
    90

    Question How to set broadcast receiver attributes programmatically in android studio?

    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:

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.mybroadcastapplication">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/Theme.MyBroadcastApplication">
            <activity
                android:name=".MainActivity"
                android:exported="true">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <receiver
                android:name=".MyBroadcastReceiver"
                android:enabled="true"
                android:exported="false">
            </receiver>
        </application>
    
    </manifest>
    My MainActivity.java file:

    Code:
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
        MyBroadcastReceiver myReceiver;
        IntentFilter intentFilter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            myReceiver = new MyBroadcastReceiver();
            intentFilter = new IntentFilter();
            intentFilter.addAction("com.example.mybroadcastapplication.EXPLICIT_INTENT");
            findViewById(R.id.button1).setOnClickListener(this);
        }
    
        public void broadcastIntent() {
            Intent intent = new Intent();
            intent.setAction("com.example.mybroadcastapplication.EXPLICIT_INTENT");
            getApplicationContext().sendBroadcast(intent);
        }
    
        @Override
        protected void onPostResume() {
            super.onPostResume();
            registerReceiver(myReceiver, intentFilter);
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            unregisterReceiver(myReceiver);
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.button1:
                    broadcastIntent();
                    break;
                default:
            }
        }
    }
    My MyBroadcastReceiver.java file:

    Code:
    public class MyBroadcastReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction() != null && intent.getAction().equals("com.example.mybroadcastapplication.EXPLICIT_INTENT"))
                Toast.makeText(context, "Explicit intent received.", Toast.LENGTH_LONG).show();
        }
    }
    Regards

  2. #2
    Join Date
    Sep 2008
    Posts
    90

    Resolved Re: How to set broadcast receiver attributes programmatically in android studio?

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured