0

in our app we have implemented several Preferences of which two have a "Button" functionality. In the onPreferenceClick() of aforementioned Preference the only thing that is done is send a Broadcast which then is actually received by a foreground Service which uses it's value to do someting with the app's foreground notification.

Unfortunately this stopped working with Android 14 Beta. When debugging I actually can see the onPreferenceClick() being called, but afterwards it's kinda of a black box. The Service does NOT receive the broadcast, and after the sendBroadcast() call within the onPreferenceClick I do not see any errors or warnings from the Android System in Logcat.

I gave a try by changing the intent passed to the sendBroadcast() to an implicit one instead of the one defined in the Preferences .xml file, but to no avail :(

<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
                                  xmlns:android="http://schemas.android.com/apk/res/android"
                                  xmlns:tools="http://schemas.android.com/tools">
<PreferenceCategory
        app:title="@string/pref_category_debug"
        app:iconSpaceReserved="false">
<nl.mycompany.myapp.preference.CheckinPreference
            android:enabled="true"
            android:key="about_button_debug_checkin"
            android:persistent="false"
            android:title="@string/about_button_checkin"
            app:iconSpaceReserved="false"
            android:layout_width="wrap_content">
        <intent android:action="mycompany.myapp.intent.action.CHECKED_IN_STATE_CHANGED">
            <extra
                    android:name="checkedin"
                    android:value="true"/>
        </intent>
    </nl.mycompany.myapp.preference.CheckinPreference>
    <nl.mycompany.myapp.preference.CheckinPreference
            android:enabled="true"
            android:key="about_button_debug_checkout"
            android:persistent="false"
            android:title="@string/about_button_checkout"
            app:iconSpaceReserved="false">
        <intent android:action="mycompany.myapp.intent.action.CHECKOUT_STATE_CHANGED">
            <extra
                    android:name="checkedin"
                    android:value="false"/>
        </intent>
    </mycompany.myapp.preference.CheckinPreference>
</PreferenceCategory>
</androidx.preference.PreferenceScreen>

This is our custom Preference :

class CheckinPreference(context: Context, attrs: AttributeSet?) : Preference(context, attrs), Preference.OnPreferenceClickListener {
override fun onPreferenceClick(preference: Preference): Boolean {
    context.sendBroadcast(intent)
    return true
}

init {
    this.onPreferenceClickListener = this
}}
TiGer
  • 5,879
  • 6
  • 35
  • 36

1 Answers1

0

I solved it myself. The problem was not in sending the Broadcast but receiving it. Whilst registering the receiver in our Service I was using the RECEIVER_NOT_EXPORTED flag, that was a mistake. After switching to the RECEIVER_EXPORTED flag the broadcast was received correctly from within the Service.

TiGer
  • 5,879
  • 6
  • 35
  • 36