Android:是否可以从共享意图打开浏览器?

jen*_*ymo 2 browser url android share

我想知道是否有可能从共享意图中打开浏览器。让我举一个例子来澄清:我有一个包含新闻的应用程序。每个新闻都有一个 url,以便用户可以通过 Whatsapp、蓝牙、环聊或其他方式与已知的 Android 共享意图共享此链接。现在我想知道用户是否也可以出于此共享意图在浏览器中打开此链接。那么:我是否能够说出他的意图,即他还应该显示在浏览器中打开新闻网址的机会?

我当前的分享意图如下所示:

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);
Run Code Online (Sandbox Code Playgroud)

小智 5

我有同样的要求。我使用下面的代码

    //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);
Run Code Online (Sandbox Code Playgroud)