Search code examples
javaandroidandroid-intentbroadcastreceiverandroid-activity

Toggle BroadcastReceiver on/off/option


I have a very standard BroadcastReceiver to intercept outgoing calls. I'd like to give the user the option to set this to off (disable the BroadcastReceiver), give me the option to use it(BroadcastReceiver is on but prompts the user to use the service or not), and always on(no prompt, the BroadcastReceiver will always trigger the activity).

Using a standard intent:

<intent-filter android:priority="1">
    <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

And the activity is generic as well:

public class CallReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        String dialedNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        String correctedNumber = correctNumber(dialedNumber);
        setResultData(correctedNumber);
    }
}

How can I achieve this? Thanks


Solution

  • You can store the users settings in the SharedPreference object. You can use a PreferenceScreen - PreferenceActivity to store the user settings. You can the read the SharedPreference object in the onReceive method. This will not completely disable the BroadcastReceiver, but it can be an okay solution for broadcast receivers that doesnt have any big impact on battery life.