如何在android中打开url

时间:2016-12-28 09:16:08

标签: android

请先阅读我的问题

我想在我的应用中打开一个不在手机浏览器中的网址。

您可以在Facebook或Twitter上看到他们如何在自己的应用中打开链接。

这将重定向到我手机的浏览器

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

但我想像Facebook App一样打开网址。

此类视图是否有任何特定的API。

5 个答案:

答案 0 :(得分:2)

我想你想使用Chrome Custom Tabs,请查看链接!

这里是网站的基本示例

// Use a CustomTabsIntent.Builder to configure CustomTabsIntent.
// Once ready, call CustomTabsIntent.Builder.build() to create a CustomTabsIntent
// and launch the desired Url with CustomTabsIntent.launchUrl()

String url = ¨https://paul.kinlan.me/¨;
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(url));

答案 1 :(得分:1)

以下代码可帮助您在应用中打开网址:

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class AppWebViewActivity extends Activity {

    private WebView mWebview;
    private String mWebViewUrl;
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        mWebview  = new WebView(this);
        mWebViewUrl= getIntent().getStringExtra("web_view_url");

        mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript

        final Activity activity = this;

        mWebview.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
            }
        });

        mWebview .loadUrl(mWebViewUrl);
        setContentView(mWebview );

    }

}

在清单中定义上述活动,如下所示:

 <activity
        android:name="<package-name>.AppWebViewActivity "
        android:screenOrientation="portrait" >
    </activity>

并添加互联网权限:

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

要调用此活动,请按以下代码操作:

Intent intent = new Intent(getBaseContext(), AppWebViewActivity .class);
intent.putExtra("web_view_url", "your-complete-url");
startActivity(intent)

您可以拥有自定义ui,在这种情况下,您必须在xml中定义webview并在webview活动中获取布局参考。

快乐编码!!!!

答案 2 :(得分:1)

您可以通过webview在您的应用中打开网址,步骤如下:

下面是webview的xml代码;

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.whiskey.servicedog.DocumentPlay"
    tools:showIn="@layout/activity_document_play">

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true">

    </WebView>

</RelativeLayout>

下面的java文件代码:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_document_play);

        webView = (WebView) findViewById(R.id.webView);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.loadUrl(urlpath);
    }

希望这可以帮助你

答案 3 :(得分:-1)

您可以在新活动中使用webview打开网址。 https://developer.android.com/guide/webapps/webview.html

答案 4 :(得分:-1)

添加webview活动/片段,并在webview中显示网站。