jennymo
jennymo

Reputation: 1480

Android: Is it possible to open the browser from a share intent?

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);

Upvotes: 2

Views: 1982

Answers (2)

Sree...
Sree...

Reputation: 343

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);

Upvotes: 5

Yi-Ping Shih
Yi-Ping Shih

Reputation: 1092

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

Upvotes: 0

Related Questions