Matt Gibson
Matt Gibson

Reputation: 38238

Sensibly setting default app (through Chooser, Preference, etc.) for ACTION_SEND text

My application will be (optionally) sending social media updates when an action is performed.

I want to do this by using the user's installed social media apps. I don't want to restrict the user to any particular set of apps, so anything that can respond to an ACTION_SEND Intent with a type of text/plain will be fine.

However, I don't want the user to have to choose every time -- typically, they'll by using the same app every time, i.e. once they've picked TweetCaster/Facebook/whatever, they'll be sticking with it.

If I just startActivity() with ACTION_SEND and text/plain, I get a "Complete action using" chooser up, and there's also a "Use by default for this action" checkbox.

But, this will set the default for that intent for all applications, and it seems (a) likely that someone might already have defaulted to a non-social-media-app for sending text, and (b) even if they haven't, it seems a bit "rude" to default such a generic intent as sending some text somewhere.

What do people generally do here? I got halfway through making a ListPreference that allows the choice of a particular application (so I can just fire off the intent at an app that's preferred for my app, not systemwide), but it seems like a lot of code for something so simple.

Am I missing something more obvious? Is there a way of firing up the chooser such that the "Use by default for this action" will only be used as the default action for my app ACTION_SENDing text/plain?

Upvotes: 2

Views: 1443

Answers (2)

zapl
zapl

Reputation: 63955

If you fire intents like ACTION_SEND without target then you don't have a way to determine what app gets to handle that event. It's up to Android to handle that better in the future.

The imo best way to implement such a functionality would be to use an ActionBar share button. They stick to the last selected app once you have used that action and you just need to press it again to share something new.

If you don't want to do that via ActionBar then you will have to do what you started - get a list of all apps that can handle ACTION_SEND and and use that app as target for your intent.

Am I missing something more obvious? Is there a way of firing up the chooser such that the "Use by default for this action" will only be used as the default action for my app ACTION_SENDing text/plain?

That chooser is a system popup, you can't access or trigger it. Setting preferred activities isn't possible via app code since you would need to use e.g. this

Edit: contrary to my beliefs you can trigger it via Intent.createChooser(Intent target, CharSequence title)

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006674

Is there a way of firing up the chooser such that the "Use by default for this action" will only be used as the default action for my app ACTION_SENDing text/plain?

No, sorry. That's one reason why most of the ACTION_SEND samples show using Intent.createChooser(), to eliminate the "make this the default" checkbox and ignore any system-wide default the user may have set.

Rather than go the ListPreference route, you could create your own chooser AlertDialog (or dialog-themed Activity) with your own "default" logic. That "default" could be:

  • a specific selection, along the lines of the system-wide "make this the default"
  • based on behavior (e.g., divide your list into "Frequently Used" and "Other" options)
  • based on something else (aggregate behavior among all your users, phase of the moon, etc.)

Creating a "super chooser" that has this sort of functionality is a medium-grade itch of mine that I will likely scratch sometime this year if nobody beats me to it.

Upvotes: 1

Related Questions