2

Is it possible to directly send a broadcast intent from a PreferenceScreen?

For example, I would like to do something like the following:

<PreferenceScreen android:title="Enable">
<intent android:action="com.otherapp.ENABLE" />
</PreferenceScreen>

But when I try this, the app FC's w/ ActivityNotFoundException.

BTW, the receiver is simply defined as:

<receiver android:name=".Receiver">
<intent-filter>
<action android:name="com.otherapp.ENABLE" />
</intent-filter>
</receiver>

This broadcast receiver has been tested to work ok, but just not from the PreferenceScreen.

TIA!

Jon
  • 21
  • 3
  • None... "no activity found to handle com.otherapp.ENABLE". It does work for Activity (when defined), but I'm hoping I can send a broadcast intent directly. – Jon Sep 12 '11 at 22:13
  • I think that in order for that to work, you have to define the intent in the intent-filter of the target's activity element in the manifest. – K-ballo Sep 12 '11 at 22:17
  • Hi, I edited the first post to show how the receiver is declared in the manifest. It's Broadcast Receiver btw, not an Activity. – Jon Sep 12 '11 at 22:21
  • Would you add the code that launchs your intent as well? – K-ballo Sep 12 '11 at 22:23
  • Not from the code, from the XML... The intention is to send a broadcast from the PreferenceScreen. The above XML can launch an Activity directly (no other code involved) for example, but not Broadcast Receiver. I'm hoping there's a simple way to do the same for a broadcast. – Jon Sep 12 '11 at 22:28

3 Answers3

5

You can extend Preference to make it send a broadcast when clicked:

public class BroadcastPreference extends Preference implements Preference.OnPreferenceClickListener {
    public BroadcastPreference(Context context, AttributeSet attrs) {
        super(context, attrs);

        this.setOnPreferenceClickListener(this);
    }

    @Override
    public boolean onPreferenceClick(Preference preference) {
        getContext().sendBroadcast(getIntent());
        return true;
    }
}

Then use your custom preference in the xml file

<com.app.example.BroadcastPreference android:title="Enable">
    <intent android:action="com.otherapp.ENABLE" />
</com.app.example.BroadcastPreference>
0

Preferences send intents to activities, not to broadcast receivers. If you want to send intents to broadcast receivers, create activity that forwards intents to broadcast receivers

public class ForwardingActivity extends Activity {
    @Override
    protected void onStart() {
        super.onStart();
        Intent incomingIntent = getIntent();
        Intent outgoingIntent = new Intent(incomingIntent);
        outgoingIntent.setComponent(null); // unblock recipients
        sendBroadcast(outgoingIntent);
    }
}

with no UI

    <activity
        android:name=".ForwardingActivity "
        android:theme="@android:style/Theme.NoDisplay" >
        <intent-filter>
            <action android:name="com.otherapp.ENABLE" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
anvish
  • 515
  • 7
  • 8
-2

I think, you should add category android.intent.category.DEFAULT to intent-filter in your manifest. It should look like this:

<receiver android:name=".Receiver">
    <intent-filter>
        <action android:name="com.otherapp.ENABLE" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>
Paul Tsupikoff
  • 1,620
  • 1
  • 14
  • 15