Programmatically register a broadcast receiver
It sounds like you want to control whether components published in your manifest are active, not dynamically register a receiver (via Context.registerReceiver()) while running.
If so, you can use PackageManager.setComponentEnabledSetting() to control whether these components are active:
http://developer.android.com/reference/android/content/pm/PackageManager.html#setComponentEnabledSetting(android.content.ComponentName, int, int)
Note if you are only interested in receiving a broadcast while you are running, it is better to use registerReceiver(). A receiver component is primarily useful for when you need to make sure your app is launched every time the broadcast is sent.
How to set broadcast receiver attributes programmatically?
You don't need to declare the BroadcastReceiver
in the manifest if you are registering/unregistering programmatically. You only need to declare BroadcastReceiver
s in the manifest if you want them to be instantiated from external triggers (for example, on device boot, or from the Alarm Manager, etc.)
How to register a receiver in the main activity?
Either you can do 2 things:
- Create and define
BroadcastReceiver
in theManifest
- Create and register the
BroadcastReceiver
in code.
For option 2 (which you are asking):
Create a BroadcastReceiver
in code (MyBroadcastReceiver).
Declare MyBroadcastReceiver in the scope of your Activity
:
MyBroadcastReceiver mMyBroadcastReceiver;
Register the BroadcastReceiver
in your Activity
by:
IntentFilter filter = new IntentFilter(android.provider.Telephony.SMS_RECEIVED);
this.registerReceiver(mMyBroadcastReceiver, filter);
Programmatically Register For BOOT_COMPLETED Broadcast
Who will start your service on boot up so that onstartcommand() would get called.?
That will not work. You have to register a receiver statically in manifest and do what you want in onReceive () of receiver.
Please take a look at this post.
How to use BroadCastReceiver programmatically
You need to add the http://developer.android.com/reference/android/content/IntentFilter.html#addDataScheme(java.lang.String):
|intent filter object|.addDataScheme("package");
The PackageManager will only send it to receivers that have that have that intent action AND the data scheme as 'package'.
How to register and unregister BroadcastReceiver programmatically?
Yes, it is possible using a ComponentName and the PackageManager.
Enabling your BroadcastReceiver:
ComponentName component = new ComponentName(context, Ringmodechange.class)
context.getPackageManager().setComponentEnabledSetting(component, PackageManager. COMPONENT_ENABLED_STATE_ENABLED , PackageManager.DONT_KILL_APP);
To disable it:
ComponentName component = new ComponentName(context, Ringmodechange.class)
context.getPackageManager().setComponentEnabledSetting(component, PackageManager. COMPONENT_ENABLED_STATE_DISABLED , PackageManager.DONT_KILL_APP);
BroadcastReceiver not triggered on boot - programmatically
It must be declared in the manifest. You are going about this the wrong way.
Rather than allowing the user to enable/disable the broadcast receiver for boot, you should handle that option within the receiver itself. Either launch the activity or not, based on the saved user preference value from the checkbox.
@Override
public void onReceive(Context context, Intent intent) {
// Handle user preference
if (!isUserPreferenceEnabled()) return;
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent activityIntent = new Intent(context, MainActivity.class);
activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activityIntent);
}
}
Related Topics
Difference Between Getcontext() , Getapplicationcontext() , Getbasecontext() and "This"
Changing Locale Within the App Itself
How to Change Fontfamily of Textview in Android
How to Implement Endless List With Recyclerview
Saving and Reading Bitmaps/Images from Internal Memory in Android
How to Draw a Path on a Map Using Kml File
Getting the Absolute File Path from Content Uri For Searched Images
How to Store Image in Sqlite Database
Re-Launch of Activity on Home Button, But...Only the First Time
How to Handle Button Clicks Using the Xml Onclick Within Fragments
Android Accelerometer Accuracy (Inertial Navigation)
Determining the Current Foreground Application from a Background Task or Service
Getting the Screen Density Programmatically in Android
Comparison of Android Networking Libraries: Okhttp, Retrofit, and Volley