Issue in Unregistering broadcast receiver

27 views Asked by At

I want my app to have statically broadcast receiver because i want it to detect incoming message while the app is closed.I have added in manifest like

<receiver android:name=".SMSReceiver"
        android:exported="true"
        android:enabled="true">

        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

now i want it to dynamically unregister by switch. when the switch is off it showed not detect incoming SMS.How i could do this. Please help me

2

There are 2 answers

0
Kaveri On

Since you can not change the manifest content dynamically.

You can use shared Preference to store the switch state as true or false .

Then inside the onRecieve , get the state from the preference and then perform your action only when state is true else do not perform.

0
takecare On

You can check a configuration value in your manifest-registered receiver and do nothing when appropriate. Example:

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (myBroadcastReceierIsOff) {
            return;
        }
        (...)
    }
}