Reputation: 21
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!
Upvotes: 2
Views: 1323
Reputation: 61
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>
Upvotes: 6
Reputation: 515
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>
Upvotes: 0
Reputation: 1650
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>
Upvotes: -2