2

I am wondering if there is a possibility to open the browser out of the share intent. Let me give an example to clarify: I have an app with news in it. Each news has an url, so that the user can share this link with the known android share intent per whatsapp, bluetooth, hangouts or something else). Now I wonder if it is possible, that the user could also open this link in the browser out of this share intent. So: am I able to tell the intent, that he should also show the opportunity to open the news-url in the browser?

My current share intent looks like the following:

Intent intent = new Intent(Intent.ACTION_SEND);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        intent.setType("text/plain");
        String shareString = news.getLink();
        intent.putExtra(Intent.EXTRA_TEXT, shareString);
        context.startActivity(intent);
jennymo
  • 1,450
  • 1
  • 18
  • 43
  • 1
    You can try [`EXTRA_INITIAL_INTENTS`](http://developer.android.com/reference/android/content/Intent.html#EXTRA_INITIAL_INTENTS) and `Intent.createChooser()` to see if you can get one chooser to show multiple disparate things. I have not tried this. – CommonsWare Mar 06 '15 at 16:33

2 Answers2

5

I had this same requirement. I used below code

    //Creating intent for sharing
    //TODO edit your share link 
    String shareString = "http://www.stackoverflow.com";
    Intent sendIntent = new Intent(Intent.ACTION_SEND);
    sendIntent.setType("text/plain");
    sendIntent.putExtra(Intent.EXTRA_TEXT, shareString);

    //Creating intent for broswer
    //TODO edit you link to open in browser
    String url = "http://www.stackoverflow.com";
    Intent viewIntent = new Intent(Intent.ACTION_VIEW);
    viewIntent.setData(Uri.parse(url));

    Intent chooserIntent = Intent.createChooser(sendIntent, "Open in...");
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{viewIntent});
    startActivity(chooserIntent);
Sree...
  • 333
  • 2
  • 7
0

This answer provides a complete example. You can choose the apps you want from the original share intent list and add your own intent.

Community
  • 1
  • 1
Yi-Ping Shih
  • 1,082
  • 12
  • 13
  • That is a bit different - but it is the start of a possible solution, in that you could run such an enumeration and then custom listing by querying for multiple intents - not just the sharing one which the browser probably does not support, but also those that it would. – Chris Stratton Mar 06 '15 at 16:45
  • I know the purposes are a little different, but the code snippet seems has provided enough information to achieve the goal. – Yi-Ping Shih Mar 06 '15 at 17:05