csdnceshi65
larry*wei
采纳率0%
2010-02-04 17:51

如何从我的应用程序打开 Android 的 web 浏览器的 URL?

已采纳

How to open an URL from code in the built-in web browser rather than within my application?

I tried this:

try {
    Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(download_link));
    startActivity(myIntent);
} catch (ActivityNotFoundException e) {
    Toast.makeText(this, "No application can handle this request."
        + " Please install a webbrowser",  Toast.LENGTH_LONG).show();
    e.printStackTrace();
}

but I got an Exception:

No activity found to handle Intent{action=android.intent.action.VIEW data =www.google.com

转载于:https://stackoverflow.com/questions/2201917/how-can-i-open-a-url-in-androids-web-browser-from-my-application

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

28条回答

  • csdnceshi74 7*4 11年前

    Try this:

    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
    startActivity(browserIntent);
    

    That works fine for me.

    As for the missing "http://" I'd just do something like this:

    if (!url.startsWith("http://") && !url.startsWith("https://"))
       url = "http://" + url;
    

    I would also probably pre-populate your EditText that the user is typing a URL in with "http://".

    点赞 24 评论 复制链接分享
  • csdnceshi51 旧行李 5年前

    Based on the answer by Mark B and the comments bellow:

    protected void launchUrl(String url) {
        Uri uri = Uri.parse(url);
    
        if (uri.getScheme() == null || uri.getScheme().isEmpty()) {
            uri = Uri.parse("http://" + url);
        }
    
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, uri);
    
        if (browserIntent.resolveActivity(getPackageManager()) != null) {
            startActivity(browserIntent);
        }
    }
    
    点赞 10 评论 复制链接分享
  • csdnceshi75 衫裤跑路 6年前

    Webview can be used to load Url in your applicaion. URL can be provided from user in text view or you can hardcode it.

    Also don't forget internet permissions in AndroidManifest.

    String url="http://developer.android.com/index.html"
    
    WebView wv=(WebView)findViewById(R.id.webView);
    wv.setWebViewClient(new MyBrowser());
    wv.getSettings().setLoadsImagesAutomatically(true);
    wv.getSettings().setJavaScriptEnabled(true);
    wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    wv.loadUrl(url);
    
    private class MyBrowser extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
    
    点赞 10 评论 复制链接分享
  • csdnceshi66 必承其重 | 欲带皇冠 5年前

    I think this is the best

    openBrowser(context, "http://www.google.com")
    

    Put below code into global class

        public static void openBrowser(Context context, String url) {
    
            if (!url.startsWith("http://") && !url.startsWith("https://"))
                url = "http://" + url;
    
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            context.startActivity(browserIntent);
        }
    
    点赞 9 评论 复制链接分享
  • weixin_41568134 MAO-EYE 7年前

    Within in your try block,paste the following code,Android Intent uses directly the link within the URI(Uniform Resource Identifier) braces in order to identify the location of your link.

    You can try this:

    Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
    startActivity(myIntent);
    
    点赞 9 评论 复制链接分享
  • csdnceshi72 谁还没个明天 6年前

    Check whether your url is correct. For me there was an unwanted space before url.

    点赞 8 评论 复制链接分享
  • weixin_41568127 ?yb? 7年前
    Intent getWebPage = new Intent(Intent.ACTION_VIEW, Uri.parse(MyLink));          
    startActivity(getWebPage);
    
    点赞 8 评论 复制链接分享
  • csdnceshi50 三生石@ 7年前

    Just like the solutions other have written (that work fine), I would like to answer the same thing, but with a tip that I think most would prefer to use.

    In case you wish the app you start to open in a new task, indepandant of your own, instead of staying on the same stack, you can use this code:

    final Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse(url));
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY|Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
    startActivity(intent);
    
    点赞 8 评论 复制链接分享
  • csdnceshi57 perhaps? 8年前

    If you want to show user a dialogue with all browser list, so he can choose preferred, here is sample code:

    private static final String HTTPS = "https://";
    private static final String HTTP = "http://";
    
    public static void openBrowser(final Context context, String url) {
    
         if (!url.startsWith(HTTP) && !url.startsWith(HTTPS)) {
                url = HTTP + url;
         }
    
         Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
         context.startActivity(Intent.createChooser(intent, "Choose browser"));// Choose browser is arbitrary :)
    
    }
    
    点赞 6 评论 复制链接分享
  • csdnceshi70 笑故挽风 10年前

    In 2.3, I had better luck with

    final Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(url));
    activity.startActivity(intent);
    

    The difference being the use of Intent.ACTION_VIEW rather than the String "android.intent.action.VIEW"

    点赞 5 评论 复制链接分享
  • csdnceshi71 Memor.の 9年前

    Try this:

    Uri uri = Uri.parse("https://www.google.com");
    startActivity(new Intent(Intent.ACTION_VIEW, uri));
    

    or if you want then web browser open in your activity then do like this:

    WebView webView = (WebView) findViewById(R.id.webView1);
    WebSettings settings = webview.getSettings();
    settings.setJavaScriptEnabled(true);
    webView.loadUrl(URL);
    

    and if you want to use zoom control in your browser then you can use:

    settings.setSupportZoom(true);
    settings.setBuiltInZoomControls(true);
    
    点赞 5 评论 复制链接分享
  • csdnceshi57 perhaps? 9年前

    other option In Load Url in Same Application using Webview

    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("http://www.google.com");
    
    点赞 5 评论 复制链接分享
  • csdnceshi71 Memor.の 8年前

    A short code version...

     if (!strUrl.startsWith("http://") && !strUrl.startsWith("https://")){
         strUrl= "http://" + strUrl;
     }
    
    
     startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(strUrl)));
    
    点赞 5 评论 复制链接分享
  • csdnceshi72 谁还没个明天 5年前

    Chrome custom tabs are now available:

    The first step is adding the Custom Tabs Support Library to your build.gradle file:

    dependencies {
        ...
        compile 'com.android.support:customtabs:24.2.0'
    }
    

    And then, to open a chrome custom tab:

    String url = "https://www.google.pt/";
    CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    CustomTabsIntent customTabsIntent = builder.build();
    customTabsIntent.launchUrl(this, Uri.parse(url));
    

    For more info: https://developer.chrome.com/multidevice/android/customtabs

    点赞 5 评论 复制链接分享
  • csdnceshi65 larry*wei 7年前
    String url = "http://www.example.com";
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);
    
    点赞 4 评论 复制链接分享
  • csdnceshi62 csdnceshi62 3年前

    //OnClick Listener

      @Override
          public void onClick(View v) {
            String webUrl = news.getNewsURL();
            if(webUrl!="")
            Utils.intentWebURL(mContext, webUrl);
          }
    

    //Your Util Method

    public static void intentWebURL(Context context, String url) {
            if (!url.startsWith("http://") && !url.startsWith("https://")) {
                url = "http://" + url;
            }
            boolean flag = isURL(url);
            if (flag) {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse(url));
                context.startActivity(browserIntent);
            }
    
        }
    
    点赞 4 评论 复制链接分享
  • csdnceshi64 游.程 4年前

    android.webkit.URLUtil has the method guessUrl(String) working perfectly fine (even with file:// or data://) since Api level 1 (Android 1.0). Use as:

    String url = URLUtil.guessUrl(link);
    
    // url.com            ->  http://url.com/     (adds http://)
    // http://url         ->  http://url.com/     (adds .com)
    // https://url        ->  https://url.com/    (adds .com)
    // url                ->  http://www.url.com/ (adds http://www. and .com)
    // http://www.url.com ->  http://www.url.com/ 
    // https://url.com    ->  https://url.com/
    // file://dir/to/file ->  file://dir/to/file
    // data://dataline    ->  data://dataline
    // content://test     ->  content://test
    

    In the Activity call:

    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(URLUtil.guessUrl(download_link)));
    
    if (intent.resolveActivity(getPackageManager()) != null)
        startActivity(intent);
    

    Check the complete guessUrl code for more info.

    点赞 4 评论 复制链接分享
  • csdnceshi71 Memor.の 7年前

    Simple, website view via intent,

    Intent viewIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://www.yoursite.in"));
    startActivity(viewIntent);  
    

    use this simple code toview your website in android app.

    Add internet permission in manifest file,

    <uses-permission android:name="android.permission.INTERNET" /> 
    
    点赞 3 评论 复制链接分享
  • csdnceshi76 斗士狗 4年前

    The response of MarkB is right. In my case I'm using Xamarin, and the code to use with C# and Xamarin is:

    var uri = Android.Net.Uri.Parse ("http://www.xamarin.com");
    var intent = new Intent (Intent.ActionView, uri);
    StartActivity (intent);
    

    This information is taked from: https://developer.xamarin.com/recipes/android/fundamentals/intent/open_a_webpage_in_the_browser_application/

    点赞 2 评论 复制链接分享
  • weixin_41568208 北城已荒凉 6年前

    Simple Answer

    You can see the official sample from Android Developer.

    /**
     * Open a web page of a specified URL
     *
     * @param url URL to open
     */
    public void openWebPage(String url) {
        Uri webpage = Uri.parse(url);
        Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }
    

    How it works

    Please have a look at the constructor of Intent:

    public Intent (String action, Uri uri)
    

    You can pass android.net.Uri instance to the 2nd parameter, and a new Intent is created based on the given data url.

    And then, simply call startActivity(Intent intent) to start a new Activity, which is bundled with the Intent with the given URL.

    Do I need the if check statement?

    Yes. The docs says:

    If there are no apps on the device that can receive the implicit intent, your app will crash when it calls startActivity(). To first verify that an app exists to receive the intent, call resolveActivity() on your Intent object. If the result is non-null, there is at least one app that can handle the intent and it's safe to call startActivity(). If the result is null, you should not use the intent and, if possible, you should disable the feature that invokes the intent.

    Bonus

    You can write in one line when creating the Intent instance like below:

    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    
    点赞 1 评论 复制链接分享
  • weixin_41568174 from.. 7年前

    a common way to achieve this is with the next code:

    String url = "http://www.stackoverflow.com";
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url)); 
    startActivity(i); 
    

    that could be changed to a short code version ...

    Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.stackoverflow.com"));      
    startActivity(intent); 
    

    or :

    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.stackoverflow.com")); 
    startActivity(intent);
    

    the shortest! :

    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.stackoverflow.com")));
    

    happy coding!

    点赞 1 评论 复制链接分享
  • csdnceshi66 必承其重 | 欲带皇冠 8年前

    You can also go this way

    In xml :

    <?xml version="1.0" encoding="utf-8"?>
    <WebView  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
    

    In java code :

    public class WebViewActivity extends Activity {
    
    private WebView webView;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);
    
        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://www.google.com");
    
     }
    
    }
    

    In Manifest dont forget to add internet permission...

    点赞 1 评论 复制链接分享
  • csdnceshi78 程序go 5年前

    Basic Introduction:

    https:// is using that one into the "code" so that no one in between can read them. This keeps your information safe from hackers.

    http:// is using just sharing purpose, it's not secured.

    About Your Problem:
    XML designing:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.example.sridhar.sharedpreferencesstackoverflow.MainActivity">
       <LinearLayout
           android:orientation="horizontal"
           android:background="#228b22"
           android:layout_weight="1"
           android:layout_width="match_parent"
           android:layout_height="0dp">
          <Button
              android:id="@+id/normal_search"
              android:text="secure Search"
              android:onClick="secure"
              android:layout_weight="1"
              android:layout_width="0dp"
              android:layout_height="wrap_content" />
          <Button
              android:id="@+id/secure_search"
              android:text="Normal Search"
              android:onClick="normal"
              android:layout_weight="1"
              android:layout_width="0dp"
              android:layout_height="wrap_content" />
       </LinearLayout>
    
       <LinearLayout
           android:layout_weight="9"
           android:id="@+id/button_container"
           android:layout_width="match_parent"
           android:layout_height="0dp"
           android:orientation="horizontal">
    
          <WebView
              android:id="@+id/webView1"
              android:layout_width="match_parent"
              android:layout_height="match_parent" />
    
       </LinearLayout>
    </LinearLayout>
    

    Activity Designing:

    public class MainActivity extends Activity {
        //securely open the browser
        public String Url_secure="https://www.stackoverflow.com";
        //normal purpouse
        public String Url_normal="https://www.stackoverflow.com";
    
        WebView webView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            webView=(WebView)findViewById(R.id.webView1);
    
        }
        public void secure(View view){
            webView.setWebViewClient(new SecureSearch());
            webView.getSettings().setLoadsImagesAutomatically(true);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
            webView.loadUrl(Url_secure);
        }
        public void normal(View view){
            webView.setWebViewClient(new NormalSearch());
            webView.getSettings().setLoadsImagesAutomatically(true);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
            webView.loadUrl(Url_normal);
    
        }
        public class SecureSearch extends WebViewClient{
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String Url_secure) {
                view.loadUrl(Url_secure);
                return true;
            }
        }
        public class NormalSearch extends WebViewClient{
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String Url_normal) {
                view.loadUrl(Url_normal);
                return true;
            }
        }
    }
    

    Android Manifest.Xml permissions:

    <uses-permission android:name="android.permission.INTERNET"/>
    

    You face Problems when implementing this:

    1. getting The Manifest permissions
    2. excess space's between url
    3. Check your url's correct or not
    点赞 评论 复制链接分享
  • csdnceshi65 larry*wei 3年前

    Try this one OmegaIntentBuilder

    OmegaIntentBuilder.from(context)
                    .web("Your url here")
                    .createIntentHandler()
                    .failToast("You don't have app for open urls")
                    .startActivity();
    
    点赞 评论 复制链接分享
  • csdnceshi70 笑故挽风 4年前

    Try this..Worked for me!

        public void webLaunch(View view) {
                WebView myWebView = (WebView) findViewById(R.id.webview);
                myWebView.setVisibility(View.VISIBLE);
                View view1=findViewById(R.id.recharge);
                view1.setVisibility(View.GONE);
                myWebView.getSettings().setJavaScriptEnabled(true);
                myWebView.loadUrl("<your link>");
    
            }
    

    xml code :-

     <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/webview"
            android:visibility="gone"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            />
    

    --------- OR------------------

    String url = "";
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);
    
    点赞 评论 复制链接分享
  • weixin_41568131 10.24 3年前

    Okay,I checked every answer but what app has deeplinking with same URL that user want to use?

    Today I got this case and answer is browserIntent.setPackage("browser_package_name");

    e.g. :

       Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
        browserIntent.setPackage("com.android.chrome"); // Whatever browser you are using
        startActivity(browserIntent);
    

    Thank you!

    点赞 评论 复制链接分享
  • csdnceshi66 必承其重 | 欲带皇冠 4年前

    This way uses a method, to allow you to input any String instead of having a fixed input. This does save some lines of code if used a repeated amount of times, as you only need three lines to call the method.

    public Intent getWebIntent(String url) {
        //Make sure it is a valid URL before parsing the URL.
        if(!url.contains("http://") && !url.contains("https://")){
            //If it isn't, just add the HTTP protocol at the start of the URL.
            url = "http://" + url;
        }
        //create the intent
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)/*And parse the valid URL. It doesn't need to be changed at this point, it we don't create an instance for it*/);
        if (intent.resolveActivity(getPackageManager()) != null) {
            //Make sure there is an app to handle this intent
            return intent;
        }
        //If there is no app, return null.
        return null;
    }
    

    Using this method makes it universally usable. IT doesn't have to be placed in a specific activity, as you can use it like this:

    Intent i = getWebIntent("google.com");
    if(i != null)
        startActivity();
    

    Or if you want to start it outside an activity, you simply call startActivity on the activity instance:

    Intent i = getWebIntent("google.com");
    if(i != null)
        activityInstance.startActivity(i);
    

    As seen in both of these code blocks there is a null-check. This is as it returns null if there is no app to handle the intent.

    This method defaults to HTTP if there is no protocol defined, as there are websites who don't have an SSL certificate(what you need for an HTTPS connection) and those will stop working if you attempt to use HTTPS and it isn't there. Any website can still force over to HTTPS, so those sides lands you at HTTPS either way


    Because this method uses outside resources to display the page, there is no need for you to declare the INternet permission. The app that displays the webpage has to do that

    点赞 评论 复制链接分享
  • csdnceshi51 旧行李 4年前

    If you want to do this with XML not programmatically you can use on your TextView:

    android:autoLink="web"
    android:linksClickable="true"
    
    点赞 评论 复制链接分享