2

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?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Matt Gibson
  • 37,886
  • 9
  • 99
  • 128

2 Answers2

1

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)

zapl
  • 63,179
  • 10
  • 123
  • 154
  • Sadly, the Action Bar doesn't quite seem to fit with what I'm doing. It's a bit of a long story, but my app is mostly interacting with the user through audio cues (possibly in the background while the app isn't even on the screen), which makes the interaction design a little unusual. I'm also targeting API level 8, so using it means some relatively fiddly work. +1 for pointing me towards it, though, as I can see it being useful in other situations. – Matt Gibson Mar 15 '12 at 14:13
1

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you. Looking into it further, I think the suggestion of a dialog-themed Activity will probably be cleaner than the way I was heading -- whether it's to create a custom chooser, or simply as a better way of building something that looks like a ListPreference but allows the choosing of applications in the preferences. (ListPreference really doesn't seem like it was designed to be abused in the ways I'm bending it, so starting from scratch instead seems more sensible, I think.) – Matt Gibson Mar 15 '12 at 14:20