Requirements:
I have a share button in my application. I have a requirement to share via Facebook. I need to indicate whether the application is installed for the native Facebook. Our solution is to send the user to facebook.com to share if the application is not installed.
Current state:
I can detect when my own application is not installed (via the package name) and adds additional intentions to the selection list.
Problem:
The item that the user must select for sharing via the "Facebook Website" says "Browser" and has an Android browser icon. The LabeledIntent element does not appear, and I get "No activity for Intent {act = android.intent.action.VIEW dat = ...}
Code (simplified ...):
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_SUBJECT, "check this out");
intent.putExtra(Intent.EXTRA_TEXT, urlToShare);
boolean facebookInstalled = false;
Intent chooser = Intent.createChooser(intent, "Share this link!");
if (!facebookInstalled)
{
Intent urlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com/sharer/sharer.php?u=" + Uri.encode(urlToShare)));
Intent niceUrlIntent = new LabeledIntent(urlIntent, context.getApplicationContext().getPackageName(), "Facebook Website", R.drawable.icon);
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[urlIntent, niceUrlIntent]);
}
context.startActivity(chooser);
Decision
, @CommonsWare, LabeledIntent , , , ACTION_VIEW Uri.
Intent myActivity = new Intent(context, ViewUriActivity.class);
myActivity.putExtra(ViewUriActivity.EXTRA_URI, "http://...");
Intent niceUrlIntent = new LabeledIntent(myActivity, context.getApplicationContext().getPackageName(), "Facebook Website", R.drawable.icon);
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{niceUrlIntent});
ViewUriActivity :
public final class ViewUriActivity extends Activity
{
public static final String EXTRA_URI = ViewUriActivity.class.getSimpleName() + "EXTRA_URI";
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent urlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getIntent().getExtras().getString(EXTRA_URI)));
startActivity(urlIntent);
finish();
}
}